【发布时间】:2017-04-14 12:43:17
【问题描述】:
我正在尝试获取组件模板中的 canvas 元素,找到了 vuejs1 的很好的文档,但没有找到 vuejs2 的文档,其中“ref”是获取元素的唯一方法。虽然我得到了对象,但是当我尝试访问变量时它是未定义的。
HTML
<div id="app>
<template id="image-capture">
<div class="row" >
<canvas ref="icanvas" ></canvas>
</div>
</template>
</div>
JS
const ic = {
template: '#image-capture' ,
created () {
console.log(this.$refs); //this returns object
console.log(this.$refs.icanvas); // but this is undefined
}
}
const routes = [
{ path: '/ic', component: ic},
]
const router = new VueRouter({
routes
})
new Vue({
router,
}).
$mount('#app')
我需要获取 icanvas 元素。
【问题讨论】:
-
给画布一个 ID 并使用它:
this.$el.querySelector('#elementID') -
不绝对不是:)
标签: vue.js vuejs2 vue-component