NativeScript Vue:组件没有在几乎相同的页面上注册在一个页面上注册,但在另一个页面上没有

如何解决NativeScript Vue:组件没有在几乎相同的页面上注册在一个页面上注册,但在另一个页面上没有

下午好,

我遇到了一个问题。希望你能帮我解决这个问题。我会试着解释这个场景。

场景

我有一个产品和类别页面。两者都导入跟踪“面包屑”的“家谱”组件(例如:水果 > 软果 > 草莓)。水果和软果是品类,草莓是产品。当前产品/类别在活动时加粗。

在这种情况下,树中的每个项目都是可点击的,如果项目是类别,则可以导航到类别页面,如果是产品,则可以导航到产品页面。

代码

正如我之前提到的,“家谱”组件在两个页面上都被导入。产品页面之前存在,因此之前在那里导入了树。树的导入方式是:

@Component({
    name: 'ProductDetails',components: {
      > familyTree: require('@/components/product-tab/_components/family-tree/family-tree.vue').default,textBlock: require('@/components/product-tab/_components/text-block/text-block.vue').default,youtubeFrame: require('@/components/product-tab/_components/youtube-frame/youtube-frame.vue').default,}
})

在Vue页面中:

<family-tree name="productTree" :current-id="productId" :family-tree="familyTree" :view-type="productViewType" /> 

这按预期工作。加载组件并显示树中的所有项目。

现在奇怪的是,对于类别页面,我实际上做了同样的事情,但是在那里我收到了未知自定义元素的错误。换句话说,组件“家谱”未被识别。

类别页面中的树导入:

@Component({
    name: 'CategoryDetails',}
})

还有.vue:

<family-tree name="categoryTree" :current-id="categoryId" :family-tree="familyTree" :view-type="categoryViewType" />

我还尝试以编程方式添加组件(带有 ref 的 stackLayout)。奇怪的是,这确实有效。

var ComponentClass = Vue.extend(FamilyTree);
        var instance = new ComponentClass({
            propsData: {
                currentId: this.categoryId,familyTree: this.familyTree,viewType: this.categoryViewType
            }
        });

        instance.$mount(); // pass nothing
        (this.$refs.container as any).appendChild(instance.$el);

我还检查了所有数据,一切都在那里。没有空指针或类似的东西。

现在通过以编程方式添加它已修复,但这不是我想要的。我只是希望能够通过 html 标签调用组件。

我希望这提供了足够的信息来帮助我。

如果您需要更多信息,请告诉我。

谢谢!

完整代码示例:

category-details.vue

<template>
    <Page backgroundColor="#EBEBEB" enableSwipeBackNavigation="true">
        <ActionBar android:flat="true" android:backgroundColor="#b6a500">
            <NavigationButton text="Go Back" android.systemIcon="ic_menu_back" @tap="$navigateBack" />
        </ActionBar>
        <ScrollView>
            <GridLayout rows="auto,*" columns="*">
                <StackLayout>
                    <StackLayout padding="10" backgroundColor="#FFF">
                        <Label :text="category.name" color="#000" class="h1" backgroundColor="#FFF" />
                        <StackLayout ref="familyTreeContainer">
                        </StackLayout>
                        <!-- TODO 2668 -->
                        <!--<family-tree name="categoryTree" :current-id="categoryId" :family-tree="familyTree" :view-type="categoryViewType" />-->
                        <Image :src="category.headlineImage ? category.headlineImageUrl : noImageUrl" height="500px" width="auto" class="bordered" />
                    </StackLayout>
                    <text-block v-for="textBlock in category.textBlocks" :text-block="textBlock"></text-block>
                    <StackLayout padding="10" marginTop="5" backgroundColor="#FFF" v-if="category.videoId">
                        <Label text="Video" color="#000" class="t-14" backgroundColor="#FFF" />
                        <youtube-frame :video-id="category.video.url"></youtube-frame>
                    </StackLayout>
                    <StackLayout padding="10" marginTop="5" backgroundColor="#FFF">
                        <Label text="Gerelateerde producten" color="#000" class="t-14" backgroundColor="#FFF" />
                        <StackLayout ref="productListContainer">
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </GridLayout>
        </ScrollView>
    </Page>
</template>

<script src="./category-details.ts"></script>

<style scoped>
    .bordered {
        border-width: 1px;
        border-color: #000;
    }

    .fontBold{
        font-weight: bold;
    }
</style>

category-details.ts

import AuthBase from '@/shared/auth-base';
import Vue from 'nativescript-vue';
import { Component,Prop } from 'vue-property-decorator';
import CategoryService from '@/services/product/category-service';
import TextBlockService from '@/services/product/text-block-service';
import { Expand,Filter } from 'odata-query';
import { ViewTypes } from '@/shared/enums/viewTypes';
import FamilyTree from '@/components/product-tab/_components/family-tree/family-tree.vue';
import ProductService from '@/services/product/product-service';
import ProductList from '@/components/product-tab/_components/product-list/product-list.vue';

@Component({
    name: 'CategoryDetails',components: {
        //TODO 2668
        /*        familyTree: require('@/components/product-tab/_components/family-tree/family-tree.vue').default,productList: require('@/components/product-tab/_components/product-list/product-list.vue').default,*/
        textBlock: require('@/components/product-tab/_components/text-block/text-block.vue').default,}
})
export default class CategoryDetails extends AuthBase {
    @Prop() readonly categoryId: number;
    @Prop() readonly familyTree: Models.FamilyTreeDto[];

    private readonly categoryService: CategoryService;
    private readonly textBlockService: TextBlockService;
    private readonly productService: ProductService;

    categoryViewType: ViewTypes = ViewTypes.Category;

    category = {} as Models.CategoryDto;

    noImageUrl: string = Vue.prototype.$noImageUrl;

    products: Models.ProductDto[] = [];

    constructor() {
        super();
        this.categoryService = new CategoryService();
        this.textBlockService = new TextBlockService();
        this.productService = new ProductService();
    }

    async mounted() {
        await this.getCategoryAsync();
        await this.getTextBlocksAsync();

        this.createFamilyTree();

        await this.getProductsAsync();
        this.createProductsList();
    }

    createFamilyTree() {
        // TODO 2668
        var ComponentClass = Vue.extend(FamilyTree);
        var instance = new ComponentClass({
            propsData: {
                currentId: this.categoryId,viewType: this.categoryViewType
            }
        });

        instance.$mount(); // pass nothing
        (this.$refs.familyTreeContainer as any).appendChild(instance.$el);
    }

    createProductsList() {
        // TODO 2668
        var ComponentClass = Vue.extend(ProductList);
        var instance = new ComponentClass({
            propsData: {
                items: this.products,}
        });

        instance.$mount(); // pass nothing
        (this.$refs.productListContainer as any).appendChild(instance.$el);
    }

    async getCategoryAsync() {
        const expand: Expand<Models.CategoryDto> = {
            headlineImage: {},video: {}
        };

        this.category = await this.categoryService.getCategoryByIdAsync(this.categoryId,expand);
    }

    async getProductsAsync() {
        const filter: Filter = {
            categoryId: this.categoryId
        };

        const expand: Expand<Models.ProductDto> = {
            headlineImage: {}
        };

        this.products = await this.productService.getProductsAsync(filter,expand,'Name');
    }

    async getTextBlocksAsync() {
        this.category.textBlocks = await this.textBlockService.getTextBlockByProductIdAsync(this.categoryId);
    }
}

编辑

问题不仅在于家谱,还在于其他组件。我有一种感觉,这与类别详细信息页面的初始化方式有关。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res