当 .js 和 .html 不在同一文件夹中时,如何在 Aurelia 中创建自定义元素?

How can I create a custom element in Aurelia when the .js and .html are not in the same folder?

我正在尝试在 Aurelia 中创建自定义元素。我使用 getViewStrategy 方法允许我的 html 和 js 文件驻留在我的应用程序的不同文件夹中,一个 js 文件夹用于 js 文件,一个 html 文件夹用于 html 文件。

我遇到的问题是,当我尝试为我的自定义元素请求这两个文件时,我收到一条错误消息,提示我正在尝试注册一个已存在同名元素的元素。

app.html

<require from="../Scripts/Templates/js/myTag.js"></require>
<require from="../Content/Templates/html/myTag.html"></require>

<my-tag></my-tag>

myTag.js

export class MyTag{
    getViewStrategy(){
        return '../../Templates/html/myTag.html';
    }
}

myTag.html

<template>
    <h4>MY TAG!!!</h4>
</template>

如何注册这个自定义元素?提前致谢。

试试 @useView 装饰器:

举个例子:https://gist.run?id=3d0a2ccf8af9b7e5b512a09f4dd6b81c

import {useView} from 'aurelia-framework';

@useView('../../../Content/Templates/html/myTag.html')
export class MyTag {
}