码迷,mamicode.com
首页 > Web开发 > 详细

在 webpack 中使用 vue-router

时间:2021-03-16 11:52:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:efault   挂载   sub   com   bsp   vuejs   account   http   isp   

1.  打开官方文档 https://router.vuejs.org/zh/installation.html

  安装: 

npm i vue-router -S
import Vue from ‘vue‘
import app from ‘./App.vue‘
// 导入 vue-router
import vueRouter from ‘vue-router‘
import account from ‘./main/account.vue‘
import goodslist from ‘./main/goodslist.vue‘

// 2. 手动安装 vueRouter
Vue.use(vueRouter)

// 3. 创建路由对象
var router = new vueRouter({
    routes: [
        { path: ‘/account‘, component: account },
        { path: ‘/goodslist‘, component: goodslist },
    ]
})

var vm = new Vue({
    el: ‘#app‘,
    render: c => c(app),
    router // 4. 将路由对象挂载到 vm 上
})

  App.vue:

<template>
    <div>
        <router-link to="/account">account</router-link>
        <router-link to="/goodslist">goodslist</router-link>
        <router-view></router-view>
    </div>
</template>
<script>
</script>
<style>
</style>

  account.vue 组件:

<template>
    <div>
        <h1>这是 Account 组件</h1>
    </div>
</template>
<script></script>
<style>
</style>

  界面效果:

技术图片

 

 2.  嵌套子路由

import Vue from ‘vue‘
import app from ‘./App.vue‘
// 导入 vue-router
import vueRouter from ‘vue-router‘
import account from ‘./main/account.vue‘
import goodslist from ‘./main/goodslist.vue‘
import login from ‘./subcom/login.vue‘
import register from ‘./subcom/register.vue‘

// 2. 手动安装 vueRouter
Vue.use(vueRouter)

// 3. 创建路由对象
var router = new vueRouter({
    routes: [
        { path: ‘/account‘,
          component: account,
          children: [
              { path: ‘login‘, component: login },
              { path: ‘register‘, component: register },
          ]
        },
        { path: ‘/goodslist‘, component: goodslist },
    ]
})

var vm = new Vue({
    el: ‘#app‘,
    render: c => c(app),
    router // 4. 将路由对象挂载到 vm 上
})

  account.vue

<template>
    <div>
        <h1>这是 Account 组件</h1>
        <router-link to="/account/login">login</router-link>
        <router-link to="/account/register">register</router-link>
        <router-view></router-view>
    </div>
</template>
<script>
</script>
<style >
</style>

  界面效果:

技术图片

 

 3. 将路由模块抽离出来

  创建一个与 main.js 平级的路由文件 router.js

 

import Vue from ‘vue‘
// 导入 vue-router
import vueRouter from ‘vue-router‘
import account from ‘./main/account.vue‘
import goodslist from ‘./main/goodslist.vue‘
import login from ‘./subcom/login.vue‘
import register from ‘./subcom/register.vue‘

// 2. 手动安装 vueRouter
Vue.use(vueRouter)

// 3. 创建路由对象
var router = new vueRouter({
    routes: [
        { path: ‘/account‘,
          component: account,
          children: [
              { path: ‘login‘, component: login },
              { path: ‘register‘, component: register },
          ]
        },
        { path: ‘/goodslist‘, component: goodslist },
    ]
})

export default router

 

  main.js :

import Vue from ‘vue‘
import app from ‘./App.vue‘
import router from ‘./router.js‘

var vm = new Vue({
    el: ‘#app‘,
    render: c => c(app),
    router // 4. 将路由对象挂载到 vm 上
})

 

在 webpack 中使用 vue-router

标签:efault   挂载   sub   com   bsp   vuejs   account   http   isp   

原文地址:https://www.cnblogs.com/bulu08042/p/14532093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!