在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1,父传子子组件中定义 父组件 <template> <div> <HelloWorld :title="msg" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, components: { HelloWorld, }, }; </script> 子组件 <template> <div class="hello"> <h1>{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props:["title"], data() { return {}; }, }; </script> 2,子传父子传父,需要在子组件中触发一个事件,在事件中,调用 子组件 <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ this.$emit("childEvent", this.age); } }, }; </script> 父组件 <template> <div> <HelloWorld @childEvent="parentEvent" :title="msg" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, methods: { parentEvent(e) { console.log(e); }, }, components: { HelloWorld, }, }; </script> 3,兄弟组件传值1,先新建一个 import Vue from 'vue'; export default new Vue; 2,在组件A中引入 <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> import bus from "../publicFn/bus.js"; export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ bus.$emit("childEvent", this.age); } }, }; </script> 3,在B组件 <template> <div id='swiper'> <button>我是按钮</button> </div> </template> <script> import bus from "../publicFn/bus.js"; export default { name:'Swiper', data (){ return { } }, mounted(){ bus.$on("childEvent", (e) => { console.log(e) }) } } </script> 4,父组件使用子组件的数据和方法1,在子组件标签上写上 2,父组件通过 父组件 <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">我是父亲</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, methods: { parentEvent() { this.$refs.hello.add(); console.log(this.$refs.hello.age); }, }, components: { HelloWorld }, }; </script> 子组件 <template> <div class="hello"> <h1>{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log("我是子组件"); } }, }; </script> 5,子组件使用父组件的数据和方法在子组件中,可以使用 父组件 <template> <div> <HelloWorld :title="msg" ref="hello" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, methods: { parentEvent() { console.log("我是父组件的方法"); }, }, components: { HelloWorld }, }; </script> 子组件 <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log(this.$parent.msg) this.$parent.parentEvent(); } }, }; </script> 6,Vuex传值
6.1,定义store import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); export default new Vuex.Store({ state: { school: "清华大学", a:"nice" }, getters: { returnVal(state) { return state.school + state.a; }, }, mutations: { changeSchool(state, val) { state.school = val; console.log('修改成功'); }, }, actions: {}, modules: {} }); 6.2,挂载 import Vue from 'vue'; import App from './App.vue'; import router from "./router"; import store from "./store"; import ElementUI from "element-ui"; import "element-ui/lib/theme-chalk/index.css"; import publicFn from "./publicFn/publicFn"; Vue.config.productionTip = false const url = process.env.VUE_APP_URL; Vue.prototype.$url = url; Vue.prototype.$publicFn = publicFn; Vue.use(ElementUI); new Vue({ router, store, render: h => h(App), }).$mount('#app') 6.3,使用 <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log(this.$store.state.school);//获取值 //this.$store.commit('changeSchool', '北京大学');//修改值 // console.log(this.$store.getters.returnVal)//获取过滤后的值 } }, }; </script> 7,路由传值7.1 通过query传值 注意:该方式刷新页面参数不丢失,并且会在地址栏后将参数显露, 页面A <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">跳转</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, methods: { parentEvent() { this.$router.push({ path:"/conter", name:'conter', query:{ id:10086, name:"鹏多多" } }) }, }, components: { HelloWorld }, }; </script> 页面B <template> <div id='conter'> </div> </template> <script> export default { name:'conter', data (){ return { } }, created (){ console.log(this.$route.query.id, this.$route.query.name); }, } </script> 7.2 通过params传值 注意:该方式刷新页面参数会丢失,可以接收后存在 A页面 <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">跳转</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "搜索音乐", }; }, methods: { parentEvent() { this.$router.push({ path:"/conter", name:"conter", params:{ id:10086, name:"鹏多多" } }) }, }, components: { HelloWorld }, }; </script> B页面 <template> <div id='conter'> </div> </template> <script> export default { name:'conter', data (){ return { } }, created (){ console.log(this.$route.params.id, this.$route.params.name); }, } </script> 到此这篇关于Vue的七种传值方式的文章就介绍到这了,更多相关Vue传值方式内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论