У меня есть простой компонент Hello:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'hello',
props: {'myprop': String},
data () {
return {
msg: 'Welcome ' + this.myprop
}
},
methods: {
mymethod: function () {
return 'hi there'
}
}
}
</script>
Теперь, в зависимости от того, как создан компонент, будут доступны методы и данные. Проще всего проиллюстрировать это с помощью теста, пожалуйста, прочитайте комментарии к тесту:
import Vue from 'vue'
import Hello from '@/components/Hello'
describe('Hello.vue', () => {
it('with Vue Extend', () => {
const Constructor = Vue.extend(Hello)
const vm = new Constructor({ propsData: { myprop: 'from test via propsdata' } }).$mount()
// following 3 expections will all SUCCEED
expect(vm.$el.querySelector('.hello h1').textContent)
.to.equal('Welcome from test via propsdata')
expect(vm.myprop).to.equal('from test via propsdata')
expect(vm.mymethod()).to.equal('hi there')
})
it('with new Vue', () => {
const vm = new Vue(
{
template: "<div><hello myprop='from template in test'></hello></div>",
components: { 'hello': Hello }
}).$mount()
// following expectation SUCCEDS
expect(vm.$el.querySelector('.hello h1').textContent)
.to.equal('Welcome from template in test')
// following TWO expections will FAIL
expect(vm.mymethod()).to.equal('hi there')
expect(vm.myprop).to.equal('from template in test')
})
})
Как я могу заставить работать последние 2 ожидания?
Спасибо.