首页 > 建站教程 > JS、jQ、TS >  详解windows下vue-cli及webpack 构建网站(四) 路由vue正文

详解windows下vue-cli及webpack 构建网站(四) 路由vue



 

1、本篇文章是建立在以上三篇文章的基础上的。

2、安装 vue-router 插件,运行cmd进入到项目目录下面,运行以下命令:

1cnpm install vue-router --save-dev

3、在src文件夹下面新建一个文件夹page用于存放模板文件,然后分别在这个文件夹下面新建 index.vue、list.vue两个文件,然后打开index.vue粘贴以下代码:

1<template>
2  
3 <div class="jumbotron">
4    <h1>这里是首页!</h1>
5 </div>
6  
7</template>

保存之后再打开list.vue粘贴以下代码:

01<template>
02  
03  <div class="list-group">
04   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item active">
05    这里是列表页
06   </a>
07   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Dapibus ac facilisis in</a>
08   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Morbi leo risus</a>
09   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Porta ac consectetur ac</a>
10   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Vestibulum at eros</a>
11  </div>
12  
13</template>

好了,两个页面的内容都准备好了,接下来我们修改入口文件app.vue的内容吧

4、打开src文件夹下面的app.vue文件,修改代码为

01<template>
02 <div>
03 <HtmlHeader></HtmlHeader>
04   <router-view
05    class="view"
06    keep-alive
07    transition
08    transition-mode="out-in">
09   </router-view>
10<HtmlFooter></HtmlFooter><span style="white-space:pre"> </span> 
11  </div>
12<span style="white-space:pre"> </span>
13</template>
14  
15  
16<script>
17import HtmlHeader from './components/header'
18import HtmlFooter from './components/footer'
19export default {
20 components: {
21  HtmlHeader,
22  HtmlFooter
23 }
24}
25</script>

这里用了 router-view 来把刚才新建的两个页面加载到这里来

修改了入口文件接下来就是要进行路由规则的配置了。

5、在src文件夹下面新建一个文件夹config用来存放路由配置,在config文件夹下面新建routes.js文件并打开,然后粘贴以下代码并保存:

01//加载模板文件
02import index from '../page/index'
03import list from '../page/list'
04//路由规则设置
05export default [
06  {
07    path: '/',
08    component: index
09  },
10  {
11    path: '/list',
12    component: list
13  }
14]

现在路由配置文件也已经配置好了,我们接下来就是要打开sec文件夹下面的main.js文件设置路由使用了

6、打开main.js 文件,在头部加入以下代码

01// 引用路由插件
02import VueRouter from 'vue-router'
03// 试用路由插件
04Vue.use(VueRouter)
05//引入路由配置文件
06import routes from './config/routes'
07// 使用配置文件规则
08const router = new VueRouter({
09 mode: 'history',
10 base: __dirname,
11 routes: routes })

这个是引入路由插件并且使用,然后加载路由规则

接着把

1new Vue({
2 el: '#app',
3 template: '<App/>',
4 components: { App }
5})

修改为

1const app = new Vue({
2   router: router,
3   render: h => h(App)
4}).$mount('#app')

设置完之后整个页面代码如图

7、加载开始运行 npm run dev 查看效果吧,打开http://localhost:8080  和http://localhost:8080/list  就可以看到不同的效果了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。