15、vue.js 之路由与子路由
来源:互联网 发布:淘宝考试题和答案2017 编辑:程序博客网 时间:2024/05/29 19:09
用node加webpack开发
1、版本1
项目文件下src下的main.js文件里面的代码如下,其他地方不动
// 这里是主文件import Vue from 'vue' // 引入vueimport Router from 'vue-router' // 引入路由插件 路径 没有 ./ / 直接写名字是找的node_modules目录下的文件const App = {// 默认模板 template: `<div id="app"> 默认显示的内容 <router-view></router-view> </div>`}const index = {// 首页模板 template: `<div>这里是index</div>`}const about = {// about页模板 template: `<div>这里是about</div>`}// 必须放在后面 因为前面声明模板用的是const ES6的语法,必须先声明后调用const router = new Router({// 跟路由显示的地方是第一个 router-view 标签里 routes: [// 在routes里面的称之为跟路由 {path: '/', component: index}, {path: '/about', component: about} ]})// 使用路由插件Vue.use(Router)Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: '#app', router, render: h => h(App)// 传递一个参数: 让页面默认显示的模板 return})
在cmd界面,在项目文件下输入npm run dev 运行环境
运行结果:手动输入网址
2、版本2
项目文件下src下的main.js文件里面的代码如下,其他地方不动
// 这里是主文件import Vue from 'vue' // 引入vueimport Router from 'vue-router' // 引入路由插件 路径 没有 ./ / 直接写名字是找的node_modules目录下的文件const App = {// 默认模板 template: `<div id="app"> 默认显示的内容 <ul> <li><router-link to="/">index</router-link></li> <li><router-link to="/about">about</router-link></li> </ul> <router-view></router-view> </div>`}const index = {// 首页模板 template: `<div>这里是index</div>`}const about = {// about页模板 template: `<div>这里是about</div>`}// 必须放在后面 因为前面声明模板用的是const ES6的语法,必须先声明后调用const router = new Router({// 跟路由显示的地方是第一个 router-view 标签里 routes: [// 在routes里面的称之为跟路由 {path: '/', component: index}, {path: '/about', component: about} ]})// 使用路由插件Vue.use(Router)Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: '#app', router, render: h => h(App)// 传递一个参数: 让页面默认显示的模板 return})
运行效果:点击切换
说明:通过<li><router-link to="/">index</router-link></li> 实现点击自动切换 通过<router-link to="/">绑定切换的地址 实际上最后会自动生成a标签
3、版本3 加入子路由
项目文件下src下的main.js文件里面的代码如下,其他地方不动
// 这里是主文件import Vue from 'vue' // 引入vueimport Router from 'vue-router' // 引入路由插件 路径 没有 ./ / 直接写名字是找的node_modules目录下的文件const App = {// 默认模板 template: `<div id="app"> 默认显示的内容 <ul> <li><router-link to="/">index</router-link></li> <li><router-link to="/about">about</router-link></li> </ul> <router-view></router-view> </div>`}const index = {// 首页模板 template: `<div>这里是index</div>`}const about = {// about页模板 template: `<div>这里是about <ul> <li><router-link to="/about/aa">aa</router-link></li> </ul> <router-view></router-view> </div>`}const aa = {// aa页模板 template: `<div>这里是aa</div>`}// 必须放在后面 因为前面声明模板用的是const ES6的语法,必须先声明后调用const router = new Router({// 跟路由显示的地方是第一个 router-view 标签里 routes: [// 在routes里面的称之为跟路由 {path: '/', component: index}, {path: '/about', component: about, children: [ // 子路不可以写 / {path: 'aa', component: aa} ]} ]})// 使用路由插件Vue.use(Router)Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: '#app', router, render: h => h(App)// 传递一个参数: 让页面默认显示的模板 return})
运行结果:
说明:
阅读全文
1 0
- 15、vue.js 之路由与子路由
- 11、vue.js 之路由
- (一) Vue 路由+子路由
- 16、vue.js 之路由传值
- 18、vue.js 之路由钩子函数
- Vue.js路由
- Vue.js两级路由
- vue框架之路由
- vue.js 底部导航栏 一级路由显示 子路由不显示
- Vue之动态路由、嵌套路由
- vue.js路由服务笔记
- 2.0vue.js 路由嵌套
- Vue.js框架路由练习
- 【21】vue.js — 路由
- 17、vue.js 之路由里的返回、前进、跳转
- vue 进阶系列之路由
- vue学习总结之路由
- vue之登录路由验证
- vue弹窗组件练习
- 找出数组中重复元素
- python 第三方库的安装
- android开发者选项里的动画的值的获取
- apk文件的内容
- 15、vue.js 之路由与子路由
- dubbo-monitor安装、 监控中心 配置过程
- linux 不能打开磁盘问题:Error mounting /dev/sda5 at /media/cc/Windows8_OS:
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- Windows下qt-creator 调试器的设置
- JAVA向上转型和向下转型
- JPA规范介绍
- Android.mk详解
- java通过链表实现队列,先进先出