Vue.js 在vue中设置子组件的道具
下面是在其组件中使用Vue模板作为剑道UI模板的示例: 该示例不太清楚如何为使用此方法渲染的组件提供属性(与在模板中正确渲染相反)。我需要向该子组件的所有实例提供在父组件中确定的单个值,并且我还需要订阅子组件发出的事件。我的假设是Vue.component()有一个重载,允许我访问此功能 编辑:Vue.js 在vue中设置子组件的道具,vue.js,kendo-ui,kendo-ui-vue,Vue.js,Kendo Ui,Kendo Ui Vue,下面是在其组件中使用Vue模板作为剑道UI模板的示例: 该示例不太清楚如何为使用此方法渲染的组件提供属性(与在模板中正确渲染相反)。我需要向该子组件的所有实例提供在父组件中确定的单个值,并且我还需要订阅子组件发出的事件。我的假设是Vue.component()有一个重载,允许我访问此功能 编辑: 具体地说,我要寻找的是一种为从Vue组件创建的每个列创建标题模板的方法。我需要每个列的模板从父级接收数据,这样我就知道如何构造它,我还需要每个列的模板向父级报告一个事件。文档确实缺乏。我同意你的看法。
具体地说,我要寻找的是一种为从Vue组件创建的每个列创建标题模板的方法。我需要每个列的模板从父级接收数据,这样我就知道如何构造它,我还需要每个列的模板向父级报告一个事件。文档确实缺乏。我同意你的看法。我采用了一种不同的方法为剑道UI组件制作模板,并实现了这一点: 首先,我有一个利用剑道下拉列表组件的下拉组件:
<template>
<div>
<p>Style with template {{template}}</p>
<kendo-dropdownlist
:template="template"
:headerTemplate="headerTemplate"
:data-source="dataSourceArray"
:data-text-field="'text'"
:data-value-field="'value'"
:filter="'contains'">
</kendo-dropdownlist>
</div>
</template>
<script>
export default {
name: "Dropdown",
props: ["dataSourceArray", "template", "headerTemplate"],
data() {
return {
value: "Click Me",
text: "I'm in Template template"
};
}
};
</script>
使用模板{{template}}的样式
导出默认值{
名称:“下拉列表”,
道具:[“数据源阵列”、“模板”、“headerTemplate”],
数据(){
返回{
值:“点击我”,
文本:“我在模板中”
};
}
};
为了呈现不同的样式/模板,我从父组件中解析了props。在这种情况下,DropdownStyles
<template>
<div id="DropdownStyles">
<h1>KendoUI dropdown instances with different templates</h1>
<Dropdown
v-for="dropdown in dropdowns"
v-bind:key="dropdown.id"
v-bind:title="dropdown.title"
v-bind:data-source-array="dropdown.dataSourceArray"
v-bind:template="dropdown.template"
v-bind:headerTemplate="dropdown.headerTemplate"
></Dropdown>
</div>
</template>
<script>
import Dropdown from "./Dropdown";
import DropdownTemplate from "./DropdownTemplate";
export default {
name: "DropdownStyles",
components: { Dropdown },
data() {
return {
dropdowns: [
{
id: 1,
title: "x style",
dataSourceArray: [
"Football",
"Tennis",
"Basketball",
"Baseball",
"Cricket",
"Field Hockey",
"Volleyball"
],
template: `<strong class="custom-dropdown">x #:data#</strong>`,
headerTemplate: DropdownTemplate.template
},
{
id: 2,
title: "+ style",
dataSourceArray: [
"Football",
"Tennis",
"Basketball",
"Baseball",
"Cricket",
"Field Hockey",
"Volleyball"
],
template: `<strong class="custom-dropdown">+ #:data#</strong>`,
headerTemplate: `<div><h3 style="padding-left:10px;">Sports 2</h3></div>`
}
]
};
}
};
</script>
具有不同模板的KendoUI下拉列表实例
从“/Dropdown”导入下拉列表;
从“/DropdownTemplate”导入DropdownTemplate;
导出默认值{
名称:“下拉式”,
组件:{Dropdown},
数据(){
返回{
下拉列表:[
{
id:1,
标题:“x风格”,
数据源阵列:[
“足球”,
“网球”,
“篮球”,
“棒球”,
“板球”,
“曲棍球”,
“排球”
],
模板:`x:data`,
headerTemplate:DropdownTemplate.template
},
{
id:2,
标题:“+风格”,
数据源阵列:[
“足球”,
“网球”,
“篮球”,
“棒球”,
“板球”,
“曲棍球”,
“排球”
],
模板:`+\:data`,
校长职位:`体育2`
}
]
};
}
};
可以将模板移动到其自己的文件或函数中。例如,第一个下拉列表使用DropdownTemplate作为其headerTemplate:
DropdownTemplate.vue
<script>
export default {
name: "DropdownTemplate",
props: ["header"],
template: `<div>
<div><h3>Sports 1</h3></div>
</div>`,
data() {
return {};
}
};
</script>
<style scoped>
h3 {
padding-left: 10px;
}
</style>
导出默认值{
名称:“下拉模板”,
道具:[“标题”],
模板:`
体育1
`,
数据(){
返回{};
}
};
h3{
左侧填充:10px;
}
我认为关键点是您所附链接中的第3步(Kendo Vue模板使用)。(以前不要碰剑道,如果有什么问题,请纠正我,谢谢。)
首先,请打开这个,你会发现一个下拉列表,然后每个选项是一个按钮加上一个文本。如果单击该按钮,它将调用MyTemplate.vue中的一个方法和DropDownStyle.vue中的另一个方法,那么每个选项的背景都是从DropDownStyle.vue传递过来的蓝色
Kendo将步骤3的这个函数绑定到它的attribute=template,然后First参数(并且只有一个)是数据源的每个元素
然后这个函数需要返回一个对象,包括template和templateArgs,然后Kendo构造它
因此,我的解决方案是将函数/回调/样式添加到templateArgs中,然后在MyTemplate.vue中执行您需要的操作
<template>
<span :style="templateArgs.styles">
<button @click="buttonClick();templateArgs.callback()">{{templateArgs.value}}</button>
{{templateArgs.text}}
</span>
</template>
<script>
export default {
name: 'template1',
methods: {
buttonClick: function (e) {
console.log('props',this.templateArgs.styles)
}
},
data () {
return {
templateArgs: {
callback:function(){
console.log('Test')
},
styles:''
}
}
}
}
</script>
以下是从步骤3扩展而来的代码
methods: {
getMyTemplate: function (e) {
// parameter=e: it is the value of each element of the dropdown
e.callback = this.eventCallback
e.styles="background-color:blue"
return {
template: MyTemplate,
templateArgs: e
}
},
eventCallback: function (data) {
console.log(this.dropdowns)
}
}
下面是MyTemplate.vue
<template>
<span :style="templateArgs.styles">
<button @click="buttonClick();templateArgs.callback()">{{templateArgs.value}}</button>
{{templateArgs.text}}
</span>
</template>
<script>
export default {
name: 'template1',
methods: {
buttonClick: function (e) {
console.log('props',this.templateArgs.styles)
}
},
data () {
return {
templateArgs: {
callback:function(){
console.log('Test')
},
styles:''
}
}
}
}
</script>
{{templateArgs.value}
{{templateArgs.text}
导出默认值{
名称:“template1”,
方法:{
按钮点击:功能(e){
console.log('props',this.templateArgs.styles)
}
},
数据(){
返回{
模板参数:{
回调:函数(){
console.log('Test')
},
样式:“”
}
}
}
}
在像他们一样传递模板方面,设计选择非常奇怪。避免使用KendoUI并专注于VueJS方法-您可以使用吗?在父母身上提供价值,在孩子身上注入价值
此外,还可以创建一个应用程序来跟踪您希望应用程序中所有组件都可用的事件或值。本质上,插件是一种服务。一个只实例化一次的单例对象。“在这种情况下是初始化的。”。。。这是什么意思?而且,在我看来,快速看一眼也没问题,您可以使用
:prop='value'
将属性传递给父元素,您可以在示例中看到这一点,这意味着我更熟悉在这样的模板中使用组件:
我不熟悉使用这些属性的方式,我不知道如何传递parms或订阅发出的事件。@MikeCole,如果你想使用内置道具,我只是想出了一种方法,但可能会让事情变得复杂。我们可以创建一个组件作为模板,对于该模板,只有一个具有一个特定类的div,如您的模板
,@MikeCole,然后在父组件中,调用let childBuilder=Vue.extend(realComponentOptions);对于(document.getElementsByClassName('your-template'){let yourChild=new childBuilder()yourChild.$mount()item.appendChild(yourChild.$el)}
要装载真正的组件(您可以将所有的孩子保存到一个数据属性中),然后通过yourChild[index]更改父组件上的道具.$props
如果我看得不正确,请告诉我,但是您是否同时为模板和头使用模板字符串