≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

分析windows下vue-cli及webpack 构建网站(四) 路由vue-router的使用

人气:409 时间:2019-04-07

这篇文章主要为大家详细介绍了分析windows下vue-cli及webpack 构建网站(四) 路由vue-router的使用,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

windows下vue-cli及webpack 构建网站(一)环境安装

 windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

windows下vue-cli及webpack 构建网站(三)使用组件 

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

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

代码如下:


cnpm install vue-router --save-dev 

【图片暂缺】

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

代码如下:


<template> 
 
 <div class="jumbotron"> 
    <h1>这里是首页!</h1> 
 </div> 
 
</template> 

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

代码如下:


<template> 
 
  <div class="list-group"> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item active"> 
    这里是列表页 
   </a> 
   <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> 
   <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> 
   <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> 
   <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> 
  </div> 
 
</template> 

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

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

代码如下:


<template> 
 <div> 
 <HtmlHeader></HtmlHeader> 
   <router-view 
    class="view" 
    keep-alive 
    transition 
    transition-mode="out-in"> 
   </router-view> 
<HtmlFooter></HtmlFooter><span style="white-space:pre"> </span>  
  </div> 
<span style="white-space:pre"> </span> 
</template> 
 
 
<script> 
import HtmlHeader from './components/header' 
import HtmlFooter from './components/footer' 
export default { 
 components: { 
  HtmlHeader, 
  HtmlFooter 
 } 
} 
</script> 

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

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

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

代码如下:


//加载模板文件 
import index from '../page/index' 
import list from '../page/list' 
//路由规则设置 
export default [ 
  { 
    path: '/', 
    component: index 
  }, 
  { 
    path: '/list', 
    component: list 
  } 
] 

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

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

代码如下:


// 引用路由插件 
import VueRouter from 'vue-router' 
// 试用路由插件 
Vue.use(VueRouter) 
//引入路由配置文件 
import routes from './config/routes' 
// 使用配置文件规则 
const router = new VueRouter({ 
 mode: 'history', 
 base: __dirname, 
 routes: routes }) 

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

接着把

代码如下:


new Vue({ 
 el: '#app', 
 template: '<App/>', 
 components: { App } 
}) 

修改为

代码如下:


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

【图片暂缺】

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

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

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

本文来自:http://www.q1010.com/184/3424-0.html

注:关于分析windows下vue-cli及webpack 构建网站(四) 路由vue-router的使用的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue实现百度搜索下拉提示功能实例
  • 基于vue2框架的机器人自动回复mini-project实例代码
  • 分析vue-router2.0动态路由获取参数
  • vue中将网页打印成pdf实例代码
  • 分析Vue.use自定义自己的全局组件
  • 浅谈 Vue v-model指令的实现原理
  • Vue.js中轻松解决v-for执行出错的三个方案
  • Vue非父子组件通信分析
  • 分析vue slot插槽的使用方法
  • Vue2.0 从零开始_环境搭建操作步骤
上一篇:分析vue跨组件通信的几种方法
下一篇:分析Vue.use自定义自己的全局组件
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。