组件传值方式
// 子组件 Child.vue methods: { clickEvent() { this.$emit('clickEvent', '我被点击了'); } }
<!--父组件 HTML--> <template> <div> <Child @clickEvent="clickEvent"></Child> </div> </template>
// 父组件 methods: { clickEvent(e) { console.log(e) // '我被点击了' } }
// 子组件 export default { props: { name: { type: String, default: '' } } }
<!--父组件 HTML--> <template> <div> <Child name="xxx"></Child> </div> </template>
// router -> index.js const routes = [ { path: '/url/:id', name: "URLDemo", component: URLDemo } ]
传值方式:
this.$router.push('/user/xxx');
获取传递过来的值:
this.route.params
// router -> index.js const routes = [ { path: '/url', name: "URLDemo", component: URLDemo } ]
传值方式:
this.$router.push({ name: 'URLDemo', params: { key: value } });
获取传递过来的值:
this.route.params // {"key":"value"}
// router -> index.js const routes = [ { path: '/url', name: "URLDemo", component: URLDemo } ]
传值方式:
this.$router.push({ path: 'URLDemo', query: { key: value } });
获取传递过来的值:
this.route.query // {"key":"value"}
<!--父组件 HTML--> <template> <div> <Child ref="child"></Child> </div> </template>
获取子组件的值:
this.$refs.child.xxx // xxx可以是子组件的方法名或变量名
this.$parent.xxx // xxx可以是父组件的方法名或变量名
import Vue from 'vue' let vueEvent = new Vue(); export default vueEvent;
// 组件一 import vueEvents from '../model/vueEvent' methods: { commit() { vueEvents.$emit('demoEvent', '我是Demo组件的值') } }
// 组件二 import vueEvents from '../model/vueEvent' created() { vueEvents.$on('demoEvent', e => { this.value = e // e == '我是Demo组件的值' }) }
window.localStorage.setItem('key',value); // 保存值 window.localStorage.getItem('key'); // 获取值