在没有使用.vue单文件的情况下,
我想点击 profile 组件里面按钮,使模态框组件显示出来,主要是修改根实例的showModal值,
开始想使用prop,发现我这个情况好像没有找到合适的列子,
使用事件发射器,不知道具体怎么使用
html 主要结构如下
<div id="app">
...
<router-view></router-view>
<!-- use the modal component, pass in the prop -->
<modal v-if="showModal" @close="showModal = false">
<!--
you can use custom content here to overwrite
default content
-->
<h3 slot="header">custom header</h3>
</modal>
</div>
<script type="text/x-template" id="profile">
<button @click="togleModal"></button>
</script>
<script type="text/x-template" id="modal-template">
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-footer">
<button class="modal-default-button" @click="$emit('close')">
OK
</button>
</div>
</div>
</div>
</div>
</transition>
</script>
js 如下,精简了一下
var bus = new Vue();
// register modal component
Vue.component('modal', {
template: '#modal-template'
});
var profile = {
template: '#profile',
data: function(){
return {
};
},
created: function () {
bus.$on('show-modal', function(flag){ //获取传递的参数并进行操作
//todo something
console.log(flag);
});
},
methods: {
}
};
var routes = [
{path: '/'},
{path: '/profile', component: profile}
];
var ROUTER = new VueRouter({
routes: routes
});
var App = new Vue({
el: '#app',
data: {
showModal: false,
uinfo: {}
},
created: function () {
bus.$emit('show-modal', this.showModal);
},
methods: {
goProfile: function(){
this.$router.history.push('/profile');
}
},
watch: {
'$route': function(to, from) {
console.log(this.$route.path);
}
},
router: ROUTER,
});
其实你只是需要组件通信?举个栗子
http://jsfiddle.net/631807682...