首页 > 其他 > 详细

模块化开发(vue-router模块)

时间:2021-09-15 12:58:19      阅读:26      评论:0      收藏:0      [点我收藏+]

vue-router模块化开发的步骤:

以vue-cli-demo为例(该项目使用的是webpack-simple模板)

技术分享图片

1、安装vue-router模块

进入项目vue-cli-demo路径, 运行 cnpm install vue-router -S (安装vue-router生产依赖)

技术分享图片

安装完成后,一是在node_modules文件夹中可以看到安装完成的vue-router模块,二是可以在package.json中自动生成了vue-router的生产依赖。

技术分享图片

2、编辑main.js

在main.js中引入vue-router模块

技术分享图片

 

3、编辑App.vue

在App.vue页面上添加路由。

技术分享图片

 4、新建components文件夹(用来存放项目中需要用到的组件)

在src文件夹下新建文件夹components,在components下新建两个.vue文件(分别命名为home.vue和news.vue)。

技术分享图片

 分别写home.vue和news.vue的代码。

技术分享图片 技术分享图片

5、新建路由配置文件router.config.js(用来配置路由)

在src文件夹下新建路由配置文件,命名为router.config.js,用来配置路由。

技术分享图片

 编写路由配置文件router.config.js中的代码。

 技术分享图片

6、在main.js中引入路由配置

增加如下代码:

import routerConfig from ‘./router.config.js‘ //3.引入路由配置
const router = new VueRouter(routerConfig); //4.创建路由实例

router:router,  //5.注入路由实例

技术分享图片


7、打开看效果

运行npm run dev ,查看路由效果。

技术分享图片  技术分享图片

 

模块化开发(vue-router模块)

原文:https://www.cnblogs.com/AnnLing/p/15251877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!