国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:42:24
字體:
來源:轉載
供稿:網友

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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 山阴县| 平利县| 裕民县| 北海市| 德昌县| 花莲市| 宣威市| 朝阳县| 交城县| 福安市| 黄梅县| 昌宁县| 宜宾市| 江津市| 当阳市| 许昌县| 南陵县| 河北省| 祁阳县| 新河县| 平舆县| 辽阳市| 博白县| 定结县| 分宜县| 洪雅县| 江门市| 南安市| 济宁市| 宜昌市| 福鼎市| 高邑县| 思南县| 南宁市| 沂源县| 卢龙县| 闽清县| 南澳县| 神农架林区| 台南县| 乐清市|