【问题标题】:How can I use dynamic components in a ListView如何在 ListView 中使用动态组件
【发布时间】:2019-07-03 07:25:44
【问题描述】:

我正在尝试使用包含提前不知道其类型的组件的模板来填充 NativeScript-Vue ListView。例如,此代码不起作用,因为 NativeScript 没有“组件”元素,但这表明我正在尝试完成:

<ListView for="component in components">
    <v-template>
        <component :is="component" />
    </v-template>
</ListView>

computed: {
    components () {
        return ['Label', 'Button'];
    }
}

是的,我知道你可以在 v-template 中使用 if="",但在这种情况下,我不知道需要在 ListView 中加载哪些组件。在我的例子中,我在插件中加载全局组件,这些组件将在 ListView 中被引用。

【问题讨论】:

    标签: vue.js nativescript nativescript-vue


    【解决方案1】:

    感谢@Manoj。那些明智的话让我想到,模板不能是动态的,但 v-template 的内容可以。也许不适合所有人,但这段代码对我有用:

    // App.vue
    
    <template>
        <Page>
            <ActionBar title="Welcome to NativeScript-Vue!"/>
            <GridLayout columns="*" rows="400,*">
                <ListView ref="lv" for="item in items">
                  <v-template>
                    <v-component :type="item.type" :options="item.options" />
                  </v-template>
                </ListView>
            </GridLayout>
        </Page>
    </template>
    
    <script lang="ts">
      import Vue from 'nativescript-vue'
      import { Component } from 'vue-property-decorator'
      import VComponent from './VComponent.vue'
    
      @Component({
        components: {
          VComponent
        }
      })
      export default class App extends Vue {
          get items () {
          return [
            {type: 'Label', options: [{key: 'text', value: 'I am a Label'}, {key: 'color', value:'red'}] },
            {type: 'Button', options: [{key: 'text', value:'I am a Button!'}]}
          ]
        }
      }
    </script>
    
    
    // VComponent.vue
    
    <template>
      <StackLayout ref="myLayout">
      </StackLayout>
    </template>
    
    <script lang="ts">
      import Vue from 'nativescript-vue'
      import { Component, Prop } from 'vue-property-decorator'
      import { StackLayout } from 'tns-core-modules/ui/layouts/stack-layout'
      import { Label } from 'tns-core-modules/ui/label'
      import { Button } from 'tns-core-modules/ui/button'
    
      const classByClassName = {
        'Label': Label,
        'Button': Button
      }
    
      @Component
      export default class CoolTemplate extends Vue {
        @Prop() type!: string;
        @Prop() options;
    
        mounted () {
          if (this.type) {
            const myLayout = <StackLayout>((<Vue>this.$refs.myLayout).nativeView)
            const component = new classByClassName[this.type]
            for (var i = 0; i< this.options.length; i++) {
              const option = this.options[i];
              component[option.key] = option.value 
            }
            myLayout.addChild(component)
          }
        }
    
      } 
    </script>
    

    【讨论】:

      【解决方案2】:

      您的模板不能是动态的,这就是使用 ListView 的全部意义所在 - 保持它们静态,以便可以根据需要重复使用它们。如果您希望根据数据查看不同的组件,则必须使用多个模板。

      Read more on docs.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-04
        • 2022-11-05
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2021-10-27
        • 2021-06-07
        • 1970-01-01
        相关资源
        最近更新 更多