laravel Blade 中的 Vue 组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43218435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vue component in blade
提问by El Hombre Sin Nombre
I′m trying to use thisin my blade view. I have .vue file and this code in js.
我正在尝试在我的刀片视图中使用它。我有 .vue 文件和 js 中的这段代码。
import Multiselect from 'vue-multiselect'
export default {
components: {
Multiselect
},
data () {
return {
value: '',
options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
}
}
}
When i add component in blade like this `
当我像这样在刀片中添加组件时`
<div>
<label class="typo__label">Single select</label>
<multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
<pre class="language-json"><code>@{{value}}</code></pre>
</div>
The select don′t work, only show {{value}} string. ?Any idea about error? `
选择不起作用,只显示 {{value}} 字符串。?任何关于错误的想法?`
回答by Joe
You need to add the parent component to the HTML too, so if you have main app.js it should look like this.
您还需要将父组件添加到 HTML 中,因此如果您有主 app.js,它应该如下所示。
// mycomponent.js
// mycomponent.js
import Multiselect from 'vue-multiselect'
export default {
components: {
Multiselect
},
data () {
return {
value: '',
options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
}
}
}
// app.js
// app.js
var MyComponent = require('./mycomponent');
var app = new Vue({
el: '#app',
components: {
MyComponent
}
});
// index.blade.php
// index.blade.php
<div id="app">
<my-component inline-template>
<div>
<label class="typo__label">Single select</label>
<multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
<pre class="language-json"><code>@{{value}}</code></pre>
</div>
</my-component>
</div>
So the "my-component" context in the html knows and tracks the value here is a fiddle so you can see it in action.
所以 html 中的“my-component”上下文知道并跟踪这里的值是一个小提琴,所以你可以看到它的运行情况。
const Multiselect = VueMultiselect.Multiselect;
var MyComponent = {
components: {
Multiselect
},
data() {
return {
value: '',
options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
}
}
};
var app = new Vue({
el: '#app',
components: {
MyComponent
}
});
<link href="https://unpkg.com/[email protected]/dist/vue-multiselect.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="app">
<my-component inline-template>
<div>
<label class="typo__label">Single select</label>
<multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
<pre class="language-json"><code>{{value}}</code></pre>
</div>
</my-component>
</div>