路由通訊是經過路由跳轉用query把參數帶過去,也是vue經常使用的通訊手段。javascript
<template>
<div id="head">
<button @click="handleChange">clickMe</button> //給按鈕綁定點擊事件
</div>
</template>
<script> export default { name: 'Head', data () { return { } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path:"/about" , query:{ text:"我是阿格斯之盾" } }) //路由跳轉,並用query帶過去參數 } } } </script> <style scoped> </style>
<template>
<div id="about">
<p>我是關於頁:{{ message }}</p><button type="button" @click="handleChange">回到首頁</button> //顯示接收過來的數據
</div>
</template>
<script> export default { name: 'About', data () { return { message: "" } }, mounted(){ this.message = this.$route.query.text //在生命週期中接收傳過來的數據 }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path: "/" }) //點擊返回首頁 } } } </script> <style scoped> </style>
import Vue from 'vue'
import Router from 'vue-router'
import Head from '@/components/Head'
import About from '@/components/About'
Vue.use(Router)
export default new Router({
mode: "history",
routes: [
{
path: '/',
name: 'Head',
component: Head
},{
path: '/about',
name: 'About',
component: About
}
]
})
仍是列舉上面的例子,將它們稍微改一改就能夠了,路由配置都同樣的。sessionStorage的特色就是瀏覽器關掉緩存就會消失,這是它區別於localStorage的。vue
1 <template>
2 <div id="head">
3 <button @click="handleChange">clickMe</button>
4 </div>
5
6 </template>
7
8 <script>
9 export default { 10 name: 'Head', 11 data () { 12 return { 13 14 } 15 }, 16 updated(){ 17 18 }, 19 methods:{ 20 handleChange(){ 21 this.$router.push({ path:"/about"}) 22 }, 23 message(){ 24 var message = "我是阿格斯之盾" 25 sessionStorage.setItem('text', message) //建立緩存 26 } 27 }, 28 mounted(){ 29 this.message(); 30 } 31 } 32 </script> 33 <style scoped> 34 35 </style>
<template>
<div id="about">
<p>我是關於頁:{{ message }}</p><button type="button" @click="handleChange">回到首頁</button>
</div>
</template>
<script>
export default { name: 'About', data () { return { message: "" } }, mounted(){ this.message = sessionStorage.getItem("text") //讀取緩存 }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path: "/" }) } } } </script> <style scoped> </style>
<template> <div id="about"> <button @click="handleChange">點擊發送消息給父組件</button> </div> </template> <script> export default { name: 'About', props:{ 'text':[] }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$emit( "child-message" , "我是阿格斯之盾" ) //提交信息 } } } </script> <style scoped> </style>
<template> <div id="head"> <About @child-message = "handleText"></About> //這裏傳過來父組件須要用一個方法接住 <p>來自子組件的消息:{{message}}</p> </div> </template> <script> import About from '@/components/About.vue' export default { name: 'Head', components:{ About }, data () { return { message : "" } }, mounted(){ }, methods:{ handleText(data){ //這裏的data就是子組件傳過來的內容 this.message = data } } } </script> <style scoped> </style>
狀態管理使用起來相對複雜,可是對於大型項目確實很是實用的。java
npm install vuex -s
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { message: '我是阿格斯之盾' }, mutations: { MESSAGE_INFO (state,view) { state.message = view; } } }) export default store
import Vue from 'vue' import App from './App' import router from './router' import store from './store' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
<template> <div id="about"> <button @click="handleChange">點擊發送消息給父組件</button> </div> </template> <script> export default { name: 'About', props:{ 'text':[] }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$store.commit("MESSAGE_INFO" , "我是火車王") //提交改變狀態 } } } </script> <style scoped> </style>
<template> <div id="head"> <About></About> <p>來自子組件的消息:{{this.$store.state.message}}</p> //直接使用this.$store.state.message接受數據顯示 </div> </template> <script> import About from '@/components/About.vue' export default { name: 'Head', components:{ About }, data () { return { message : "" } }, mounted(){ }, methods:{ } } </script> <style scoped> </style>