• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    vue中各種通信傳值方式總結

    來源:懂視網 責編:小采 時間:2020-11-27 22:00:55
    文檔

    vue中各種通信傳值方式總結

    vue中各種通信傳值方式總結:1、路由通信傳值 路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段。 例子:創建并在路由注冊一個組件Head <template> <div id=head> <button @click=handleChange>clickMe<
    推薦度:
    導讀vue中各種通信傳值方式總結:1、路由通信傳值 路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段。 例子:創建并在路由注冊一個組件Head <template> <div id=head> <button @click=handleChange>clickMe<

    1、路由通信傳值

    路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段。

    例子:創建并在路由注冊一個組件Head

    <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>
    

    創建另一個組件About并在路由注冊

    <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
     }
     ]
    })
    

    2、sessionStorage本地緩存通信

    還是列舉上面的例子,將它們稍微改一改就可以了,路由配置都一樣的。sessionStorage的特點就是瀏覽器關掉緩存就會消失,這是它區別于localStorage的。

    例子: Heade代碼:

    <template>
     <div id="head">
     <button @click="handleChange">clickMe</button>
     </div>
     
    </template>
    
    <script>
    export default {
     name: 'Head',
     data () {
     return {
     
     }
     },
     updated(){
     
     },
     methods:{
     handleChange(){
     this.$router.push({ path:"/about"})
     },
     message(){
     var message = "我是阿格斯之盾"
     sessionStorage.setItem('text', message) //創建緩存
     }
     },
     mounted(){
     this.message();
     }
    }
    </script>
    <style scoped>
    
    </style>
    

    About代碼:

    <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>
    

    3、父組件向子組件通信

    定義父組件Head,還是用上面的例子,父組件傳遞一句話給子組件,如果傳遞的參數很多,可使用json數組{}的形式。

    例子: Head父組件代碼

    <template>
     <div id="head">
     <About :text=message></About> //將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>
    

    About子組件代碼

    <template>
     <div id="about">
     <p>我是關于頁:{{ text }}</p>
     </div>
    </template>
    
    <script>
    
    export default {
     name: 'About',
     props:{
     'text':[] //子組件接受數據,[]里面可以寫傳入類型,如果不符合會報錯
     },
     data () {
     return {
     message: ""
     }
     },
     mounted(){
     
     },
     updated(){
     
     },
     methods:{
     handleChange(){
     
     }
     }
    }
    </script>
    <style scoped>
    
    </style>
    

    4、子組件向父組件通信 子組件向父組件通信是通過emit事件發送的,話不多說,直接上案例,還是利用上面的案例稍作修改 About子組件代碼:

    <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>
    

    Head父組件代碼

    <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>
    

    5、vuex狀態管理

    狀態管理使用起來相對復雜,但是對于大型項目確實非常實用的。

    (1)安裝vuex,并建立倉庫文件

    npm install vuex -s

    安裝過后在src文件中創建store文件夾,并建立index.js文件,index.js的代碼如下:

    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

    (2)在min.js中注冊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/>'
    })
    

    (3)狀態的讀取和提交 還是使用上面的案例,我們以子組件About提交改變狀態,父組件Head接受狀態并顯示出來下面是About組件提交狀態

    <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>
    

    Head組件接受狀態:

    <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>
    

    總結:

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    vue中各種通信傳值方式總結

    vue中各種通信傳值方式總結:1、路由通信傳值 路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段。 例子:創建并在路由注冊一個組件Head <template> <div id=head> <button @click=handleChange>clickMe<
    推薦度:
    標簽: VUE 方式 通信
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲国产精品无码久久久蜜芽| 亚洲欧洲自拍拍偷精品 美利坚| 国产三级精品三级在专区| 久久精品免费一区二区三区| 久久99精品国产| 91在线手机精品超级观看| 四虎影视884a精品国产四虎| 国产成人久久精品二区三区| 国产小呦泬泬99精品| 国产精品原创巨作?v网站| 国产精品美女一区二区视频| 99久久99这里只有免费费精品| 国内精品伊人久久久久AV影院| 国产成人精品午夜福利| 亚洲精品乱码久久久久久不卡| 精品国产福利一区二区| 全国精品一区二区在线观看| 无码人妻精品中文字幕免费| 99re6这里有精品热视频| 中文字幕精品无码久久久久久3D日动漫 | 精品视频一区二区三区免费| 久久久久久夜精品精品免费啦 | 欧美精品丝袜久久久中文字幕| 国产一区二区精品尤物| 97久久精品人人做人人爽| 亚洲七七久久精品中文国产| 欧美人与动牲交a欧美精品| 久久99精品国产99久久6| 中文精品无码中文字幕无码专区| 1024国产欧美日韩精品| 久久久久99精品成人片试看| 亚洲精品蜜桃久久久久久| 亚洲精品国精品久久99热| 亚洲国产精品一区二区第四页| 久久丝袜精品中文字幕 | 野狼第一精品社区| 亚洲日韩国产精品第一页一区| 亚洲中文字幕久久精品无码喷水| 亚洲国产精品成人一区 | 99久久精品费精品国产一区二区 | 国产精品无码不卡一区二区三区|