Vue工程添加组件调用

App.vue

<template>
  <div id="nav">
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/test1">Test1</router-link>
  </div>
  <router-view/>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 10px;
  background-color: lightskyblue;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color:orangered;
}
</style>

index.js

import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/test1',
    name: 'Test1',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Test1.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

Test1.vue

<template>
  <div class="test1">
    <h1>This is a test1 page</h1>
    <Test1Top msg="Welcome to Test1Top"/>
    <Test1Center msg="Welcome to Test1Center"/>
    <Test1Bottom msg="Welcome to Test1Bottom"/>
  </div>
</template>

<script>
// @ is an alias to /src
import Test1Top from '@/components/Test1Top.vue';
import Test1Center from '@/components/Test1Center.vue';
import Test1Bottom from '@/components/Test1Bottom.vue';

export default {
  name: 'Test1',
  data(){
    return{

    }
  },
  components: {
    Test1Top,Test1Center,Test1Bottom
  }
}
</script>

Test1Top.vue

<template>
  <div>
    Test1Top
    <input type="text" placeholder="请输入">
  </div>
</template>

<script>
export default {
  name: 'Test1Top'
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

Test1Center.vue

<template>
<div>
  Test1Center
  <div>
     <table border="1px;" width="80%;">
       <tr>
         <th>1</th>
         <th>2</th>
         <th>3</th>
       </tr>
       <tr>
         <td>a</td>
         <td>b</td>
         <td>c</td>
       </tr>
     </table>
  </div>
</div>
</template>

<script>
export default {
  name: 'Test1Center'
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

Test1Bottom.vue

<template>
  <div>
    Test1Bottom
    <span>共1条</span>
  </div>

</template>

<script>
export default {
  name: 'Test1Bottom'
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
原文地址:https://www.cnblogs.com/mingforyou/p/15200926.html