这篇文章主要介绍了关于vue活动创建项目之路由设计及导航栏的开发 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
我们直接开始项目,像引入Element-ui这种基础操作我在这里就跳过了
根据对项目的分析,我新建了如下几个组件。![1531107413647393.png 1771663699-5b42191f47059_articlex[1].png](https://img.php.cn//upload/image/371/846/993/1531107413647393.png)
组件新建好以后,我们来设置路由
src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Index from 'components/Index'
import Login from 'components/Login'
import Regular from 'components/activity/regular/Regular'
import Topic from 'components/activity/topic/Topic'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'index',
component: Index
},
{
path: '/login',
component: Login
},
{
path: '/Topic',
component: Topic
},
{
path: '/regular',
component: Regular
}
]
})这里要注意的就是我import的路径是经过设置的
在build/webpack.base.conf.js找到resolve,将我们components设置为我们组件的位
这样子在import的时候components就代表了‘src/components’路径
立即学习“前端免费学习笔记(深入)”;
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
'components': resolve('src/components'),
}
}因为用的是Muse-ui所以导航栏直接从文档里copy,这里就不上代码了,使用方法文档里写的很清楚了
这里就说一下涉及Vue语法的部分,项目顶部导航栏左边的title要求随路由变化而变化,在Vue里有watch监听器,我们通过watch来监听$route的变化来实现这个效果
Nav.vue
export default {
name: 'Nav',
data () {
return {
nowRouter: this.$route.name
}
},
watch: {
$route (to, from) {
this.nowRouter = this.$route.name
}
}
}设置好这些,在控制台运行命令npm run dev我们看看效果
![1531107431695504.png 2062750250-5b421932a7935_articlex[1].png](https://img.php.cn//upload/image/346/186/564/1531107431695504.png)
可以看到页面雏形已经搭建出来了
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上就是Vue活动创建项目之路由设计及导航栏的开发的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号