Vue.js 在组件外设置数据

Jen*_*sen 5 javascript vue.js

我有一个组件,我想在组件外设置一个数据项。

我将如何使用指令做到这一点?

我在想这样的事情:

Vue.directive('init', {
    bind: function(el, binding, vnode) {
        vnode.context[binding.arg] = binding.value;
    }
});
Run Code Online (Sandbox Code Playgroud)

但它不起作用:

Vue.directive('init', {
    bind: function(el, binding, vnode) {
        vnode.context[binding.arg] = binding.value;
    }
});
Run Code Online (Sandbox Code Playgroud)
Vue.directive('init', {
  bind: function(el, binding, vnode) {
    vnode.context[binding.arg] = binding.value;
  }
});

Vue.component('test', {
	template: '<p>{{date}}</p>',
  data() {
  	return {
    		date: ""
    }
  }
});

new Vue({
  el: '#app'
})
Run Code Online (Sandbox Code Playgroud)

知道我如何让它发挥作用吗?

- 编辑 -

我不想为此使用道具!我有很多很多领域。

Ikb*_*bel 3

这应该有效。

Vue.directive('init', {
  bind: function(el, binding, vnode) {
    let component = el.__vue__
    component[binding.arg] = binding.value
  }
});

Vue.component('test', {
  template: '<p>{{date}}</p>',
  data() {
    return {
      date: ""
    }
  }
})

new Vue({
  el: '#app'
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.1/vue.js"></script>


<div id="app">
  <test v-init:date="'2017-07-20 09:11:42'"></test>
</div>
Run Code Online (Sandbox Code Playgroud)