国产探花免费观看_亚洲丰满少妇自慰呻吟_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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 永定县| 克什克腾旗| 南丰县| 谷城县| 黔西县| 阆中市| 枞阳县| 阿巴嘎旗| 宁安市| 保山市| 民丰县| 芒康县| 洪雅县| 新化县| 古丈县| 会昌县| 武城县| 尼木县| 湛江市| 广昌县| 嘉峪关市| 乌恰县| 安化县| 扎兰屯市| 永仁县| 余江县| 沽源县| 浦城县| 云安县| 碌曲县| 普安县| 玉山县| 双辽市| 南京市| 淅川县| 义马市| 南投县| 汉寿县| 鄯善县| 定安县| 治多县|