JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello bus!</h1>
<p>{{i}}</p>
<my-demo />
</div>
CSS
.router-link-active {
color: red;
}
Babel + JSX
const bus = new Vue({
data () {
return {
val: {
result: 0
}
}
},
created () {
this.$on('update1', val => {
this.val.result = val
})
}
})
const Demo1 = Vue.component('MyDemo', {
template: `<div>
<h1>demo1</h1>
<div>data中获取直接修改值:{{dataResult}}</div>
<div>data中获取修改值的父层:{{dataVal}}</div>
<div>computed中依赖直接修改值:{{computedResult}}</div>
</div>`,
data () {
return {
dataResult: bus.val.result,
dataVal: bus.val
}
},
computed: {
computedResult() {
return bus.val.result
}
}
})
// app
const app = new Vue({
data () {
return {
i: 0
}
},
created () {
setInterval(() => {
bus.$emit('update1', this.i++)
}, 100)
}
}).$mount('#app')