######store
の値をローカルデータに代入してそれをwatch
で監視しているのですが、store
の値が変更してもwatch
が発火しません。
######直接this
で書き込んでもwatch
内ではthis
を使ってはいけないとエラーが出ます。
######どのような書き方をすれば、store
内の値をwatch
で監視できますか?
#store
側のコード
creative.js
export const state = () => ({ template: { id: 2, width: 200, height: 200, layers: [ { type: 'logo', width: 50, height: 50, x_position: 10, y_position: 5, z_index: 1 }, { type: 'image', width: 20, height: 20, x_position: 10, y_position: 100, z_index: 0 }, { type: 'image', width: 60, height: 30, x_position: 10, y_position: 150, z_index: 2 }, ], }, colors: { logo: '#FF0000', text: '#00FF00', image: '#0000FF', }, list_flag: '', }) export const mutations = { setTemplate(state, template) { state.template.id = template.id state.template.width = template.width state.template.height = template.height state.template.layers = template.layers }, setSize(state, size) { state.template.width = size.width state.template.height = size.height if (state.template.layers) { state.template.layers = [] } }, setTypeColor(state, type) { switch (type) { case 'logo': return state.colors.logo break case 'text': return state.colors.text break case 'image': return state.colors.image break } }, setFlag(state, flag) { state.list_flag = flag } } export const actions = { writeTemplate(context, template) { context.commit('setTemplate', template) }, writeSize(context, size) { context.commit('setSize', size) }, changeTypeColor(context, type) { context.commit('setTypeColor', '#00FF00') }, changeFlag(context, flag) { context.commit('setFlag', flag) }, } export const getters = {}
#現在のコード
一部抜粋
data() { return { //ローカルでstoreのstateデータを代入 template: this.$store.state.creative.template } }, watch: { //watchでローカルデータのtemplateが変更した際にメソッドを発火させる template: function () { this.draw() } }
#.vueの全体コード
preview.vue
<template> <div> <div style="width:800px;height:300px;padding:50px;border:solid 1px #DDD;background-color:#EFEFEF;overflow:scroll"> <canvas :style="{ border: 'solid 1px #DDD' }"></canvas> </div> </div> </template> <script> export default { data() { return { //ローカルでstoreのstateデータを代入 template: this.$store.state.creative.template } }, watch: { //watchでローカルデータのtemplateが変更した際にメソッドを発火させる template: function () { this.draw() } }, mounted() { this.ctx = this.$el.firstChild.firstElementChild.getContext('2d') this.draw() }, methods: { draw() { console.log('ikeruyo') this.ctx.canvas.width = this.template.width this.ctx.canvas.height = this.template.height this.ctx.beginPath() this.ctx.clearRect(0,0,this.template.width, this.template.height) this.template.layers.forEach(layer => { this.ctx.strokeStyle = this.test(layer.type) this.ctx.strokeRect(layer.x_position, layer.y_position, layer.width, layer.height) }); this.ctx.fill() }, createImage() { console.log(this.template) html2canvas(document.querySelector("#preview")).then(function(canvas){ let result = document.querySelector("#result") result.innerHTML = '' let image = result.appendChild(canvas) let canvasImage = document.getElementById("result").firstElementChild let link = document.createElement("a") link.href = canvasImage.toDataURL("image/png") link.download = "test.png" link.click() }) }, test: function(t) { switch ( t ) { case 'text': return '#CC7777' break; case 'logo': return '#77CC77' break; case 'image': return '#7777CC' break; } } }, } </script>
change.vue
<template> <div> <div v-for="size in creative_data.sizes"> <el-button class="size" :style="{ width: size.width / 3 + 'px', height: size.height / 3 + 'px', }" @click="$store.dispatch('creative/writeSize', size)" > {{ size.width }}×{{ size.height }} </el-button> </div> </div> </template> <script> export default { props: { }, data() { return { radius: 3, } }, computed: {}, components: {}, data() { return { creative_data: { sizes: [ { width: '300', height: '400', }, { width: '300', height: '100', }, { width: '400', height: '500', }, { width: '200', height: '200', }, { width: '100', height: '500', }, ], }, } }, mounted: function() {}, methods: { }, } </script> <style scoped lang="scss"> @import '../../../../assets/css/creative/size.css'; </style>
稚拙な画像なのですが、現在の自分のデータ間構成についてです。
shou6さんが貼ってくださった通りのコードだとwatch呼び出しに成功しているのですが、自分のコードの場合watch呼び出しできませんでした。
説明すると、「change.vue」でstoreのstate内データのtemplateの値を変更しています。watch処理を行っていない別ファイルで。(変更処理はstoreのactions等を使ってcommitしています)
憶測なのですが、自身のファイル内で変更の行われない値は監視されないのでしょうか?
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2018/12/21 08:28