zoukankan      html  css  js  c++  java
  • Vue(十六)vue-router路由

    一、 vue-router路由

     
    1. 简介
    使用Vue.js开发SPA(Single Page Application)单页面应用
    根据不同url地址,显示不同的内容,但显示在同一个页面中,称为单页面应用

    [参考](https://router.vuejs.org/zh-cn)

    bower info vue-router
    cnpm install vue-router -S

    2. 基本用法
    a.布局
    b.配置路由
    	<style>
    		.active{
    			font-size:20px;
    			color:red;
    			text-decoration: none;
    		}
    	</style>
    
    	<script src="https://unpkg.com/vue"></script>
    	<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    
    	<script>
    		window.onload = function(){
    			//1.定义组件
    			var Home = {
    				template:'<h3>我是主页</h3>'
    			}
    
    			var News = {
    				template:'<h3>我是新闻</h3>'
    			}
    
    			//2.配置路由
    			const routes = [
    				{path:'/home',component:Home},
    				{path:'/news',component:News},
    				{path:'*',redirect:'/home'} //重定向
    			]
    
    			//3.创建路由实例
    			const router = new VueRouter({
    				mode:'history', //更改模式
    				routes, //简写,相当于routes:routes
    				linkActiveClass:'active'
    			})
    			
    			//4.创建根实例并将路由挂载到Vue实例上
    			var vm = new Vue({
    				el:'#app',
    				router //注入路由
    			})
    		}
    	</script>
    
    </head>
    
    <body>
    
        <div id="app">
    		
    		<div>
    			<!-- 使用 router-link 组件来导航. -->
    			<!-- 通过传入 `to` 属性指定链接. -->
    			<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
    			<router-link to="/home">主页</router-link>
    			<router-link to="/news">新闻</router-link>
    		</div>
    
    		<div>
    			<!-- 路由出口 -->
    			<!-- 路由匹配到的组件将渲染在这里 -->
    			<router-view></router-view>
    		</div>
    	</div>
    
    </body>
    

      

      

     

    3. 路由嵌套和参数传递
    传参的两种形式:
    a.查询字符串:login?name=tom&pwd=123
    {{$route.query}}
    b.rest风格url:regist/alice/456
    {{$route.params}}
    	
    	<style>
    		.active{
    			font-size:20px;
    			color:red;
    			text-decoration: none;
    		}
    	</style>
    
    	<script src="https://unpkg.com/vue"></script>
    	<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    
    	<script>
    		window.onload = function(){
    			//1.定义组件
    			var Home = {
    				template:'<h3>我是主页</h3>'
    			}
    
    			var User = {
    				template:'#user'
    			}
    			
    			var Login={
    				template:'<h4>用户登陆。。。获取参数:{{$route.query.pwd}}</h4>'
    			}
    
    			var Regist={
    				template:'<h4>用户注册。。。获取参数:{{$route.params}}</h4>'
    			}
    
    			const routes = [
    				{
    					path:'/home',
    					component:Home
    				},
    				{
    					path:'/user',
    					component:User,
    					children:[
    						{
    							path:'login',
    							component:Login
    						},
    						{
    							path:'regist/:username/:password',
    							component:Regist,
    						}
    					]
    				},
    				{
    					path:'*',
    					redirect:'/home'
    				}
    			]
    
    			const router = new VueRouter({
    				routes,
    				linkActiveClass:'active' //更新活动链接的class类名
    			})
    			
    			var vm = new Vue({
    				el:'#app',
    				router //注入路由
    			})
    		}
    	</script>
    
    </head>
    
    <body>
    
        <div id="app">
    		
    		<div>
    			<!-- 使用 router-link 组件来导航. -->
    			<!-- 通过传入 `to` 属性指定链接. -->
    			<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
    			<router-link to="/home">主页</router-link>
    			<router-link to="/user">用户</router-link>
    		</div>
    
    		<div>
    			<!-- 路由出口 -->
    			<!-- 路由匹配到的组件将渲染在这里 -->
    			<router-view></router-view>
    		</div>
    
    	</div>
    
    	<template id="user">
    		<div>
    			<h3>用户信息</h3>
    			<ul>
    				<router-link to="/user/login?name=Tom&pwd=123" tag="li">用户登录</router-link>
    				<router-link to="/user/regist/alice/456" tag="li">用户注册</router-link>
    			</ul>
    			<router-view></router-view>
    		</div>
    	</template>
    
    </body>
    

      

      

    4. 路由实例的方法
    router.push() 添加路由,功能上与<route-link>相同
    router.replace() 替换路由,不产生历史记录
    		new Vue({
    			el:'#itany',
    			router, //注入路由
    			methods:{
    				push(){
    					router.push({path:'home'}); //添加路由,切换路由
    				},
    				replace(){
    					router.replace({path:'user'}); //替换路由,没有历史记录
    				}
    			}
    		});
    

      


    5. 路由结合动画(使用前面讲到animate.css)
             <div>
    			<router-link to="/home">主页</router-link>
    			<router-link to="/user">用户</router-link>
    		</div>
    		<div>
    			<transition enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutRight">
    				<router-view></router-view>
    			</transition>
    		</div>    
    

      



    二、 单文件组件

    1. .vue文件
    .vue文件,称为单文件组件,是Vue.js自定义的一种文件格式,一个.vue文件就是一个单独的组件,在文件内封装了组件相关的代码:html、css、js

    .vue文件由三部分组成:<template>、<style>、<script>
    <template>
      html
    </template>

    <style>
      css
    </style>

    <script>
      js
    </script>

    2. vue-loader
    浏览器本身并不认为.vue文件,所以必须对.vue文件进行加载解析,此时需要vue-loader
    类似的loader还有许多,如:html-loader、css-loader、style-loader、babel-loader等
    需要注意的是vue-loader是基于webpack的

    3. webpack
    webpack是一个前端资源模板化加载器和打包工具,它能够把各种资源都作为模块来使用和处理
    实际上,webpack是通过不同的loader将这些资源加载后打包,然后输出打包后文件
    简单来说,webpack就是一个模块加载器,所有资源都可以作为模块来加载,最后打包输出

    [官网](http://webpack.github.io/)

    webpack版本:v1.x v2.x

    webpack有一个核心配置文件:webpack.config.js,必须放在项目根目录下

    4. 示例,步骤:
     
    4.1 创建项目,目录结构 如下:
    webpack-demo
    |-index.html
    |-main.js 入口文件
    |-App.vue vue文件
    |-package.json 工程文件
    |-webpack.config.js webpack配置文件
    |-.babelrc Babel配置文件

    4.2 编写App.vue

    4.3 安装相关模板
    cnpm install vue -S

    cnpm install webpack -D
    cnpm install webpack-dev-server -D

    cnpm install vue-loader -D
    cnpm install vue-html-loader -D
    cnpm install css-loader -D
    cnpm install vue-style-loader -D
    cnpm install file-loader -D

    cnpm install babel-loader -D
    cnpm install babel-core -D
    cnpm install babel-preset-env -D //根据配置的运行环境自动启用需要的babel插件
    cnpm install vue-template-compiler -D //预编译模板

    合并:cnpm install -D webpack webpack-dev-server vue-loader vue-html-loader css-loader vue-style-loader file-loader babel-loader babel-core babel-preset-env vue-template-compiler

    4.4 编写main.js

    4.5 编写webpack.config.js

    4.6 编写.babelrc

    4.7 编写package.json

    4.8 运行测试
    npm run dev


    三、 vue-cli脚手架

    1. 简介
    vue-cli是一个vue脚手架,可以快速构造项目结构
    vue-cli本身集成了多种项目模板:
    simple 很少简单
    webpack 包含ESLint代码规范检查和unit单元测试等
    webpack-simple 没有代码规范检查和单元测试
    browserify 使用的也比较多
    browserify-simple

    2. 示例,步骤:
     
    2.1 安装vue-cli,配置vue命令环境
    cnpm install vue-cli -g
    vue --version
    vue list

    2.2 初始化项目,生成项目模板
    语法:vue init 模板名 项目名

    2.3 进入生成的项目目录,安装模块包
    cd vue-cli-demo
    cnpm install

    2.4 运行
    npm run dev //启动测试服务
    npm run build //将项目打包输出dist目录,项目上线的话要将dist目录拷贝到服务器上

    3. 使用webpack模板
    vue init webpack vue-cli-demo2

    ESLint是用来统一代码规范和风格的工具,如缩进、空格、符号等,要求比较严格
    [官网](http://eslint.org)

    问题Bug:如果版本升级到node 8.0 和 npm 5.0,控制台会报错:
    GET http://localhost:8080/__webpack_hmr net::ERR_INCOMPLETE_CHUNKED_ENCODING
     
     
    解决方法:
    a)降低Node版本到7.9或以下
    b)修改build/dev-server.js文件,如下:
    var hotMiddleware = require('webpack-hot-middleware')(compiler, {
    log: () => {},
    heartbeat:2000 //添加此行
    })
    参考:https://github.com/vuejs-templates/webpack/issues/731

     

  • 相关阅读:
    文本数据清洗总结
    PyTorch
    PyTorch
    NLP
    TF
    TF
    TF
    cairosvg
    word2vec 实现 影评情感分析
    Gensim
  • 原文地址:https://www.cnblogs.com/yulingjia/p/8328528.html
Copyright © 2011-2022 走看看