Last active
August 26, 2015 03:43
-
-
Save nasum/26b2f21a272e9803955d to your computer and use it in GitHub Desktop.
Vue.jsで親ViewModelにcomponentをぶら下げてデータを共有 ref: http://qiita.com/tomato360/items/1244cb44c832dda7f7d5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Component Test</h1> | |
<div class="main"> | |
{{title}} | |
<my-component-one data="{{$data}}"></my-component-one> | |
<my-component-two data="{{$data}}"></my-component-two> | |
<button v-on="click:output"> | |
check | |
</button> | |
</div> | |
<div class="output"> | |
</div> | |
<my-component></my-component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dataModel = { | |
title: "Title", | |
one: "component1", | |
two: "component2" | |
}; | |
var MyComponent_one = Vue.extend({ | |
template: '<p v-on="click:rewrite">{{data.one}}</p>', | |
props: ['data'], | |
methods:{ | |
rewrite: function(){ | |
this.data.one = "rewrite"; | |
} | |
} | |
}); | |
var MyComponent_two = Vue.extend({ | |
template: '<p v-on="click:rewrite">{{data.two}}</p>', | |
props: ['data'], | |
methods:{ | |
rewrite: function(){ | |
this.data.two = "rewrite"; | |
} | |
} | |
}); | |
var mainViewModel = new Vue({ | |
el: ".main", | |
data: dataModel, | |
methods: { | |
output: function(){ | |
$('.output').html(this.$data.one + " " + this.$data.two); | |
} | |
}, | |
components: { | |
"my-component-one": MyComponent_one, | |
"my-component-two": MyComponent_two | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment