Blogger Information
Blog 53
fans 3
comment 0
visits 55537
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue-route实现路由
邯郸易住宋至刚
Original
1945 people have browsed it

Vue实现路由的三大步骤

本例是使用$ vue init webpack projectname 命令创建的项目

一、创建备用组件

在项目根目录vue下有一个src目录,src下有一个components文件夹,创建组件默认放在这个文件下,我创建了三个组件:home.vue,film.vue,card.vue

代码如下:

1、home.vue

  1. <template>
  2. <div>
  3. <h2>我是home组件</h2>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "home"
  9. }
  10. </script>
  11. <style scoped>
  12. </style>

2、film.vue

  1. <template>
  2. <div>
  3. <h2>我是film组件</h2>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "film"
  9. }
  10. </script>
  11. <style scoped>
  12. </style>

3、card.vue

  1. <template>
  2. <div>
  3. <h2>我是card组件</h2>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "card"
  9. }
  10. </script>
  11. <style scoped>
  12. </style>

二、在指定App中添加组件路由标签<router-view></router-view>

1、查找指定App

本框架入口文件是main.js,打开文件查找指定App,如图

2、添加组件路由标签<router-view></router-view>

这里指定的App就是“myapp”,打开myapp,引入组件,如图

三、在router文件夹下index.js文件中配置路由

1、导入组件

  1. import home from '@/components/home'
  2. import film from '@/components/film'
  3. import card from '@/components/card'

2、配置路由

  1. routes: [
  2. {
  3. path: '/home',
  4. component: home
  5. },
  6. {
  7. path: '/film',
  8. component: film
  9. },
  10. {
  11. path: '/card',
  12. component: card
  13. }
  14. ]

四、结果如图

1、http://localhost:8080/#/home

2、http://localhost:8080/#/film

3、http://localhost:8080/#/card

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments