vue(學(xué)習(xí)筆記三)——vue知識點匯總

Vue簡介

2014年誕生,2013年react,09年angularjs

作者: 尤雨溪

核心概念: 組件化 雙向數(shù)據(jù)流(基于ES5中的defineProperty來實現(xiàn)的),IE9才支持

angular核心: 模塊化 雙向數(shù)據(jù)綁定(臟檢測:一個數(shù)組($watch),性能弱)

開發(fā)一個登陸模塊,登陸需要顯示的頭部、底部、中部

組件:組合起來的一個部件(頭部、底部、中部)

細分代碼

頭部: 頁面、樣式、動態(tài)效果

代碼: templete style script

數(shù)據(jù)流

1向:js內(nèi)存屬性發(fā)生改變,影響頁面的改變

1向:頁面的改變影響js內(nèi)存屬性的改變

Vue實例對象

// 構(gòu)造函數(shù)

var my = new Vue({

el: '#app', // 掛載點 (設(shè)置vue對象裝載到頁面位置)

template: '<div> {{ fruit }} </div>', // 模板

data: { // 數(shù)據(jù)

? ? fruit: 'apple'

}

});

data中的屬性會被代理到 my 對象中,可以使用 my.fruit 來獲取屬性值

vue常用指令

v-text

v-html

v-if

v-show

v-model

v-bind

代碼

<template>

<!-- 只能有一個根節(jié)點 -->

? <div>

? ? <pre>

? ? ? ? ?* v-text 是元素的innerText 只能在雙標(biāo)簽元素中使用

? ? ? ? * v-html 是元素的innerHTML 不能包含<!-- {{ xxx }} -->

? ? ? ? * v-if? 元素是否移出或插入

? ? ? ? * v-show 元素是否隱藏或顯示

? ? ? ? * v-model雙向數(shù)據(jù)綁定

? ? ? ? * v-bind 單向數(shù)據(jù)綁定(內(nèi)存js改變影響頁面,頁面改變不影響內(nèi)存js)

? ? </pre>

? ? v-text:

? ? <span v-text="text"></span>

? ? <hr />

? ? v-html:

? ? <span v-html="html"></span>

? ? <hr />

? ? v-if:

? ? <div v-if="isShow" style="height: 100px; background-color:red;"></div>

? ? <hr />

? ? v-show:

? ? <div v-show="isShow" style="height: 100px; background-color:green;"></div>

? ? <hr />

? ? v-model:

? ? <input type="text" name="username" v-model="username" />

? ? {{ username }}<br/>

? ? <!--給下面的input value賦值 用 v-bind:value="username" -->

? ? <input type="text" name="" v-bind:value="username"? />

? ? <hr />

? </div>

</template>

<script>

export default {

? ? data(){

? ? ? ? return {

? ? ? ? ? ? text: '我是v-text內(nèi)容',

? ? ? ? ? ? html: `

? ? ? ? ? ? ? ? <ul>

? ? ? ? ? ? ? ? ? ? <li>哈哈</li>

? ? ? ? ? ? ? ? ? ? <li>呵呵</li>

? ? ? ? ? ? ? ? </ul>

? ? ? ? ? ? `,

? ? ? ? ? ? isShow: false,

? ? ? ? ? ? username: 'admin'

? ? ? ? }

? ? }

}

</script>

<style>

</style>

class結(jié)合v-bind使用

需要根據(jù)可變的表達式的結(jié)果來給class賦值,就需要用到v-bind:class=”xxx”

v-bind:屬性名=”表達式”,最終表達式運算結(jié)束的結(jié)果賦值給該屬性?

簡化的寫法:?:屬性名="表達式"

class: 結(jié)果的分類

一個樣式: 返回字符串(三元表達式和 key和樣式的對象清單)

多個樣式: 返回對象(樣式做key,true或false做值)

<template>

? <div>

? ? <div v-bind:class="isRed? 'red' : 'green'">單個class樣式</div>

? ? <ul>

? ? ? ? <!-- 當(dāng)stu.score = A 時 匹配red,當(dāng)stu.score= B 時匹配green? -->

? ? ? ? <li v-for="stu in stus" :class="{'A': 'red', 'B': 'green'}[stu.score]"> {{ stu.name }}</li>

? ? </ul>

? ? <div :class="{'red': true, 'big': true}">多個class樣式</div>

? </div>

</template>

<script>

export default {

? ? data(){

? ? ? ? return {

? ? ? ? ? ? isRed: false,

? ? ? ? ? ? stus: [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'jack',

? ? ? ? ? ? ? ? ? ? score: 'A'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'lucy',

? ? ? ? ? ? ? ? ? ? score: 'B'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ]

? ? ? ? }

? ? }

}

</script>

<style>

.red{

? ? background-color: red;

}

.green{

? ? background-color: green;

}

.big{

? ? font-size: 30px;

}

</style>

methods 和 v-on的使用

綁定事件的方法?

????????v-on:事件名="表達式||函數(shù)名"

????????簡寫:@事件名="表達式||函數(shù)名"

函數(shù)名如果沒有參數(shù),可以省略() 只給一個函數(shù)名稱

函數(shù)的聲明需要在export default 這個對象的根屬性加上 methods 屬性中

凡是在template中使用函數(shù)或變量,不需要使用this

v-on高級用法

修飾符:

.stop - 調(diào)用 event.stopPropagation()。

.prevent - 調(diào)用 event.preventDefault()。

.capture - 添加事件偵聽器時使用 capture 模式。

.self - 只當(dāng)事件是從偵聽器綁定的元素本身觸發(fā)時才觸發(fā)回調(diào)。

.{keyCode | keyAlias} - 只當(dāng)事件是從特定鍵觸發(fā)時才觸發(fā)回調(diào)。

.native - 監(jiān)聽組件根元素的原生事件。

.once - 只觸發(fā)一次回調(diào)。

.left - (2.2.0) 只當(dāng)點擊鼠標(biāo)左鍵時觸發(fā)。

.right - (2.2.0) 只當(dāng)點擊鼠標(biāo)右鍵時觸發(fā)。

.middle - (2.2.0) 只當(dāng)點擊鼠標(biāo)中鍵時觸發(fā)。

.passive - (2.3.0) 以 { passive: true } 模式添加偵聽器

用法:

綁定事件監(jiān)聽器。事件類型由參數(shù)指定。表達式可以是一個方法的名字或一個內(nèi)聯(lián)語句,如果沒有修飾符也可以省略。

從 2.4.0 開始,v-on 同樣支持不帶參數(shù)綁定一個事件/監(jiān)聽器鍵值對的對象。注意當(dāng)使用對象語法時,是不支持任何修飾器的。

用在普通元素上時,只能監(jiān)聽 原生 DOM 事件。用在自定義元素組件上時,也可以監(jiān)聽子組件觸發(fā)的自定義事件。

在監(jiān)聽原生 DOM 事件時,方法以事件為唯一的參數(shù)。如果使用內(nèi)聯(lián)語句,語句可以訪問一個 event屬性:v?on:click="handle(′ok′,event屬性:v?on:click="handle(′ok′,event)”。

<!-- 對象語法 (2.4.0+) -->

<button v-on="{ mousedown: doThis, mouseup: doThat }"></button>

<!-- 內(nèi)聯(lián)語句 -->

<button v-on:click="doThat('hello', $event)"></button>

<!-- 縮寫 -->

<button @click="doThis"></button>

<!-- 停止冒泡 -->

<button @click.stop="doThis"></button>

<!-- 阻止默認(rèn)行為 -->

<button @click.prevent="doThis"></button>

<!-- 阻止默認(rèn)行為,沒有表達式 -->

<form @submit.prevent></form>

<!--? 串聯(lián)修飾符 -->

<button @click.stop.prevent="doThis"></button>

<!-- 鍵修飾符,鍵別名 -->

<input @keyup.enter="onEnter">

<!-- 鍵修飾符,鍵代碼 -->

<input @keyup.13="onEnter">

<!-- 點擊回調(diào)只會觸發(fā)一次 -->

<button v-on:click.once="doThis"></button>

<template>

? <div>

? ? <button v-on:click="isRed = !isRed">按鈕</button>

? ? <button v-on:click="change()">按鈕</button>

? ? <button @click="change">按鈕</button>

? </div>

</template>

<script>

export default {

? ? data(){

? ? ? ? return {

? ? ? ? ? ? isRed: false,

? ? ? ? ? ? stus: [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'jack',

? ? ? ? ? ? ? ? ? ? score: 'A'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'lucy',

? ? ? ? ? ? ? ? ? ? score: 'B'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ]

? ? ? ? }

? ? },

? ? // 聲明函數(shù),屬于組件對象

? ? methods: {

? ? ? ? // 包含多個函數(shù)名稱做key,函數(shù)提供做value

? ? ? ? change (){

? ? ? ? ? ? this.isRed = !this.isRed;

? ? ? ? ? ? this.stus.push({

? ? ? ? ? ? ? ? name: 'mick',

? ? ? ? ? ? ? ? score: 'A'

? ? ? ? ? ? });

? ? ? ? }

? ? }

}

</script>

<style>

.red{

? ? background-color: red;

}

.green{

? ? background-color: green;

}

.big{

? ? font-size: 30px;

}

</style>

v-for的使用

可以使用操作數(shù)組(item,index)

可以使用操作對象(value,key,index)

key 是類似于trank by的屬性,為了告訴vue,js中的元素和頁面的關(guān)聯(lián),當(dāng)刪除元素的時候,是單個元素的刪除而不是整版的替換,所有需要其關(guān)聯(lián)關(guān)系。2.0版本后必輸設(shè)置(性能)

<template>

? <div>

? ? <ul>

? ? ? ? <li v-for="(stu, index) in stus" v-bind:key="index">

? ? ? ? ? ? index:{{ index }} - stu:{{ stu }}

? ? ? ? </li>

? ? ? ? 使用對象的方式-滾動歌詞(時間做key,內(nèi)容作為value)<hr />

? ? ? ? <li v-for="(value, key, index) in person" v-bind:key="index">

? ? ? ? ? ? value:{{ value }}-key:{{ key }}-index:{{ index }}

? ? ? ? </li>

? ? </ul>

? </div>

</template>

<script>

export default {

? ? data(){

? ? ? ? return {

? ? ? ? ? ? isRed: false,

? ? ? ? ? ? stus: [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'jack',

? ? ? ? ? ? ? ? ? ? score: 'A'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? name: 'lucy',

? ? ? ? ? ? ? ? ? ? score: 'B'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ],

? ? ? ? ? ? person: {

? ? ? ? ? ? ? ? name: 'zhangsan',

? ? ? ? ? ? ? ? realname: '張三'

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

</script>

<style>

.red{

? ? background-color: red;

}

.green{

? ? background-color: green;

}

.big{

? ? font-size: 30px;

}

</style>

簡單學(xué)生添加刪除案例

<template>

? <div>

? ? <ul>

? ? ? ? <li v-for="(stu, index) in stus" :key="stu.id" :class="{'A':'red','B':'blue','C':'green','D':'pink'}[stu.score]">

? ? ? ? ? ? {{ stu.name }} - {{ stu.score }}? ?

? ? ? ? ? ? <button @click="del(index)">刪除</button>

? ? ? ? </li>

? ? </ul>

? ? 學(xué)生姓名:<input type="text" name="name" v-model="name"/><br/>

? ? 學(xué)生成績:<input type="text" name="score" v-model="score"/><br/>

? ? <button @click="addStu">添加學(xué)生</button>

? </div>

</template>

<script>

export default {

? ? data(){

? ? ? ? return {

? ? ? ? ? ? isRed: false,

? ? ? ? ? ? name:'',

? ? ? ? ? ? score:'',

? ? ? ? ? ? stus: [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? id: 1,

? ? ? ? ? ? ? ? ? ? name: '張三',

? ? ? ? ? ? ? ? ? ? score: 'A'

? ? ? ? ? ? ? ? },{

? ? ? ? ? ? ? ? ? ? id: 2,

? ? ? ? ? ? ? ? ? ? name: '張無忌',

? ? ? ? ? ? ? ? ? ? score: 'B'

? ? ? ? ? ? ? ? },{

? ? ? ? ? ? ? ? ? ? id: 3,

? ? ? ? ? ? ? ? ? ? name: '趙敏',

? ? ? ? ? ? ? ? ? ? score: 'C'

? ? ? ? ? ? ? ? },{

? ? ? ? ? ? ? ? ? ? id: 4,

? ? ? ? ? ? ? ? ? ? name: '殷素素',

? ? ? ? ? ? ? ? ? ? score: 'D'

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ]

? ? ? ? }

? ? },

? ? // 聲明函數(shù),屬于組件對象

? ? methods: {

? ? ? ? // 添加

? ? ? ? addStu (){

? ? ? ? ? ? // 獲取頁面輸入的值:v-model

? ? ? ? ? ? this.stus.push({

? ? ? ? ? ? ? ? id: 1,

? ? ? ? ? ? ? ? name: this.name,

? ? ? ? ? ? ? ? score: this.score

? ? ? ? ? ? });

? ? ? ? ? ? this.name = '';

? ? ? ? ? ? this.score = '';

? ? ? ? },

? ? ? ? // 刪除

? ? ? ? del (index){

? ? ? ? ? ? this.stus.splice(index, 1);

? ? ? ? }

? ? }

}

</script>

<style>

.red{

? ? background-color: red;

}

.green{

? ? background-color: green;

}

.blue{

? ? background-color: skyblue;

}

.pink{

? ? background-color: hotpink;

}

</style>

父子組件使用(父傳子)

父需要聲明子組件,引入子組件對象,聲明方式如下:

// 引入子組件

import 子組件對象名 from './xxx.vue';

// 聲明子組件

components: {

// 組件名(在模板中使用): 組件對象

}

全局組件,使用更為方便,不需要引入和聲明直接使用

在main.js中引入異常,在main.js中使用?vue.component('組件名',組件對象);

聲明為全局組件后,就可以直接通過組件名使用

父組件向自組件傳遞數(shù)據(jù)

父組件通過子組件標(biāo)簽屬性將值傳遞?

? ??????方式一:常量?<header-vue 屬性名="常量值"></header-vue>

????????方式二:變量<header-vue :屬性名="變量名"></header-vue>

子組件使用該屬性值需要使用props 聲明?

? ??????在根屬性加?props: ['屬性名1','屬性名2'...]

????????在頁面中就可以直接使用?{{ 屬性名 }}

????????在js中可以直接使用 this.屬性名 訪問

????????export default{ data(){return{ } },

? ????????????// 接受父組件參數(shù)的設(shè)置

????????????? props:['textbody']

????????}

子組件向父組件通信(vuebus)

????????通過new Vue() 的一個對象,來$on(‘事件名’, fn(prop1, prop1)) 綁定事件

? ??????另一個組件引入統(tǒng)一個vuebus,來$emit(‘事件名’,prop1, prop2) 觸發(fā)事件

vue高級

? ? `? ?vue過濾器

? ? ? ? 獲取dom元素

????????mint-ui

????????vue組件的使用

? ? ? ? 組件間通信

????????vue-router使用

????????vue-resource發(fā)起http請求

????????axios

vue過濾器

content | 過濾器, vue中沒有提供默認(rèn)過濾器,需要我們自定義過濾器

組件內(nèi)過濾器 + 全局過濾器?

????組件內(nèi)過濾器就是options中的一個filters的屬性(一個對象)?

? ? ????多個key就是不同的過濾器名,多個value就是與key對應(yīng)的函數(shù)體

? ??Vue.filter(名, fn)

????如果名稱相同以局部為主

app.vue

<template>

? <div>

? ? 請輸入內(nèi)容:

? ? <input type="text" name="name" v-model="name"/>

? ? 顯示:{{ name | myFilter}}

? </div>

</template>

<script>

? ? export default {

? ? ? ? // 自定義過濾器

? ? ? ? filters: {

? ? ? ? ? ? myFilter: function(value){

? ? ? ? ? ? ? ? // 輸入的內(nèi)容翻轉(zhuǎn): 轉(zhuǎn)換為數(shù)組->翻轉(zhuǎn)數(shù)組 ->轉(zhuǎn)換為字符串

? ? ? ? ? ? ? ? return value.split('').reverse().join('');

? ? ? ? ? ? }

? ? ? ? },

? ? ? ? data(){

? ? ? ? ? ? return {

? ? ? ? ? ? ? ? name:''

? ? ? ? ? ? }

? ? ? ? }

? ? }

</script>

<style scoped>

</style>

main.js

// 全局過濾器Vue.filter('myFilter1', function(value){

? ? return value.split('').reverse().join(',');

});

獲取DOM元素

前端框架就是為了減少dom操作,特定情況下也提供了的操作方式

在指定的元素上,添加ref=”名稱”

在獲取的地方加入 this.$refs.名稱?

????如果ref放在了原生DOM元素上,獲取的數(shù)據(jù)就是元素DOM對象

? ??如果ref放在組件對象上,獲取的就是組件對象

? ??獲取子組件DOM對象,通過this.refs.sub.refs.sub.el

事件

? ??created 完成數(shù)據(jù)初始化,未生成DOM

? ??mounted 將數(shù)據(jù)已經(jīng)裝載到DOM之上,且DOM生成完畢

<template>

? <div>

? ? <sub-vue ref="sub"></sub-vue>

? ? <div ref="myDiv"></div>

? </div>

</template>

<script>

? ? import SubVue from './components/sub.vue';

? ? export default {

? ? ? ? data(){

? ? ? ? ? ? return {

? ? ? ? ? ? }

? ? ? ? },

? ? ? ? components: {

? ? ? ? ? ? SubVue: SubVue

? ? ? ? },

? ? ? ? // 組件創(chuàng)建后,數(shù)據(jù)已經(jīng)完成初始化,但DOM還未完成

? ? ? ? created (){ // 事件的處理函數(shù)(created)

? ? ? ? ? ? console.log(this.$refs.sub);? ? ? ? ? // vue的組件對象

? ? ? ? ? ? this.$refs.sub.$el.innerHTML = '哈哈';? // 獲取vue組件對象對應(yīng)的DOM對象

? ? ? ? ? ? console.log(this.$refs.myDiv);? ? ? ? // undefined 獲取不到

? ? ? ? },

? ? ? ? // 數(shù)據(jù)裝載到DOM上后,各種數(shù)據(jù)已經(jīng)就位,將數(shù)據(jù)渲染到DOM上,DOM已經(jīng)生產(chǎn)

? ? ? ? mounted (){ // 事件的處理函數(shù)(created)

? ? ? ? ? ? console.log(this.$refs.myDiv);// 獲取的原生DOM對象

? ? ? ? ? ? this.$refs.myDiv.innerHTML = '案發(fā)生的發(fā)生';

? ? ? ? }

? ? }

</script>

<style scoped>

</style>

vue-router

前端路由 核心就是錨點值的改變,根據(jù)不同的值,渲染指定DOM位置的不同數(shù)據(jù)

ui-router(anglar):錨點值改變,通過ajax獲取模板

vue中,模板數(shù)據(jù)不是通過ajax請求來的,而是調(diào)用函數(shù)獲取到模板內(nèi)容

vue核心插件:?

? ??vue-router 路由

????vuex 管理全局共享數(shù)據(jù)

使用方式?

? ??1: 下載 npm install vue-router -S

????2: 引入 impot Router from 'vue-router'

????3: 安裝插件 Vue.use(Router)

????4: 創(chuàng)建路由對象并配置路由規(guī)則

????5: 將其李洋老師對象傳遞給Vue實例,options中

????6: 留坑

命名路由

需求:通過a標(biāo)簽單擊實現(xiàn)頁面跳轉(zhuǎn)

使用 標(biāo)簽

<a href="#/music">進入音樂</a>

<a href="#/movie">進入電影</a>

// 以上直接通過a標(biāo)簽方式直接指定路徑名稱,如果錨點發(fā)生改變不好維護

<!-- 根據(jù)name跳轉(zhuǎn)(推薦使用該方式)-->

<router-link :to="{name: 'Music'}">進入音樂</router-link>

<router-link :to="{name: 'Movie'}">進入電影</router-link>

<!-- 根據(jù)path跳轉(zhuǎn)-->

<router-link to="/movie">進入電影</router-link>

router-link 參數(shù)傳遞

????vue-router 中掛在了兩個對象

? ? route(信息數(shù)據(jù))router(功能函數(shù))

? ??參數(shù)傳遞兩種方式:?

????????1:查詢字符串 query:{key: value} -> /detail?id=1


<router-link :to="{name: 'Detail',query: {id: user.id}}"> 查看詳情 </router-link>

? ? 路由path不用改:

? ? {

? ? ? path: '/detail',

? ? ? name: 'Detail',

? ? ? component: Detail

? ? }

2: 路徑字符串 params:{key: value} -> /detial/1

<router-link :to="{name: 'Detail',params: {id: user.id}}"> 查看詳情 </router-link>

? ? 路由path需要改:

? ? {

? ? ? path: '/detail/:id',

? ? ? name: 'Detail',

? ? ? component: Detail

? ? }

獲取參數(shù)?

????this.$route.query.id

????this.$route.params.id

編程式導(dǎo)航

不能保證用戶一定會單擊某些按鈕.

并且當(dāng)前操作除了路由跳轉(zhuǎn)以外,還有一些別的附加操作

this.$router.go 根據(jù)瀏覽器記錄 前進 1 后退-1

this.$router.push(直接跳轉(zhuǎn)到某個頁面顯示)?

push參數(shù): 字符串 /xxx

push參數(shù): 對象 {name: ‘xxx’,query:{id:1}, params:{id:1,name:’zhangsan’}}

注意:有params的路由規(guī)則中一定記得在路由規(guī)則path中添加 path: ‘/movie/:id’,

<template>

? <div >

? ? ? ? <button @click="goMusic">跳轉(zhuǎn)到music</button>

? ? ? ? <button @click="goMovie">跳轉(zhuǎn)電影</button>

? ? ? ? <button @click="goBack">跳轉(zhuǎn)到上一頁</button>

? ? ? ? <button @click="testQuery">編程導(dǎo)航傳遞參數(shù)query方式</button>

? ? ? ? <button @click="testParams">編程導(dǎo)航傳遞參數(shù)params方式</button>

? </div>

</template>

<script>

export default {

? data () {

? ? return {

? ? }

? },

? methods: {

? ? goMusic (){

? ? ? ? this.$router.push('/music');

? ? },

? ? goMovie (){

? ? ? ? this.$router.push({

? ? ? ? ? ? name: 'Movie' // 路由規(guī)則 name值

? ? ? ? });

? ? },

? ? goBack (){

? ? ? ? this.$router.go(-1); //-1 上一次瀏覽器記錄 1 下一個瀏覽器記錄

? ? },

? ? testQuery (){

? ? ? ? ? ? // 查詢字符串方式: /music?id=1&name=zhagnsan

? ? ? ? ? ? this.$router.push({

? ? ? ? ? ? ? ? name: 'Music', // 路由規(guī)則 name值

? ? ? ? ? ? ? ? query: {

? ? ? ? ? ? ? ? ? ? id: 1,

? ? ? ? ? ? ? ? ? ? name: 'zhagnsan'

? ? ? ? ? ? ? ? }

? ? ? ? ? ? });

? ? },

? ? testParams (){

? ? ? ? ? ? // 查詢字符串方式: /movie/1

? ? ? ? ? ? this.$router.push({

? ? ? ? ? ? ? ? name: 'Movie', // 路由規(guī)則? name值

? ? ? ? ? ? ? ? params: {? ? ? // 路由規(guī)則? path: '/movie/:id',

? ? ? ? ? ? ? ? ? ? id: 1,

? ? ? ? ? ? ? ? ? ? name: 'zhagnsan'

? ? ? ? ? ? ? ? }

? ? ? ? ? ? });

? ? }

? }

}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>

</style>

原生監(jiān)聽錨點值改變

window.addEventListener('hashchange', function(){

? ? var text = '';// 可以換成模板數(shù)據(jù)

? ? switch(location.hash){

? ? ? ? case '#/music':

? ? ? ? ? ? text = '各種音樂數(shù)據(jù)';

? ? ? ? ? ? break;

? ? ? ? case '#/movie':

? ? ? ? ? ? text = '各種電影數(shù)據(jù)';

? ? ? ? ? ? break;

? ? }

? ? document.getElementById('content').innerHTML = text;

});

重定向和404

重定向(寫死路徑名)?{path: '/', redirect: '/home'}

重定向(使用name) {path: '/', redirect: {name: 'home'}}

404:在路由規(guī)則的最后一個規(guī)則中寫一個很強大的匹配?

????{path: '*', component: notFoundVue}

? ??notFoundVue.vue: 404頁面組件

多視圖模式

? ??以前一個路由對應(yīng)一個

? ??現(xiàn)在一個路由可以對應(yīng)多個

? ??使用components 實現(xiàn)多視圖模式

<router-view name="header"></router-view>

? ? <router-view ></router-view>? 沒有name使用default

? ? <router-view name="footer"></router-view>

? ? routes: [

? ? ? ? {

? ? ? ? ? path: '/',

? ? ? ? ? name: 'Home',

? ? ? ? ? // 注意這里名稱為components

? ? ? ? ? components: {

? ? ? ? ? ? header: HeaderVue,

? ? ? ? ? ? footer: FooterVue,

? ? ? ? ? ? default: Home

? ? ? ? ? }

? ? ? ? }

? ? ]

嵌套路由

借助 vue-router,使用嵌套路由配置,就可以很簡單地表達這種關(guān)系。

????用單頁實現(xiàn)多頁應(yīng)用,使用復(fù)雜的嵌套路由完成

????開發(fā)中一般都會用到嵌套路由

????視圖包含視圖

? ? 路由父子級關(guān)系路由配置

routes: [

{ path: '/user/:id', component: User,

children: [

{

// 當(dāng) /user/:id/profile 匹配成功,

// UserProfile 會被渲染在 User 的 <router-view> 中

path: 'profile',

component: UserProfile

},

{

// 當(dāng) /user/:id/posts 匹配成功

// UserPosts 會被渲染在 User 的 <router-view> 中

path: 'posts',

component: UserPosts

}

]

}

]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # 傳智播客vue 學(xué)習(xí)## 1. 什么是 Vue.js* Vue 開發(fā)手機 APP 需要借助于 Weex* Vu...
    再見天才閱讀 3,790評論 0 6
  • 相關(guān)概念 混合開發(fā)和前后端分離 混合開發(fā)(服務(wù)器端渲染) 前后端分離后端提供接口,前端開發(fā)界面效果(專注于用戶的交...
    他愛在黑暗中漫游閱讀 3,022評論 4 45
  • vue筆記 一.vue實例 vue的生命周期 beforeCreate(創(chuàng)建前), created(創(chuàng)建后), b...
    秋殤1002閱讀 1,130評論 0 1
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,178評論 0 29
  • 今天是爸爸開工的第一天 今天下了2018年的第一場雨 風(fēng)調(diào)雨順,媽媽這么說的
    L小青閱讀 391評論 0 0

友情鏈接更多精彩內(nèi)容