代码之家  ›  专栏  ›  技术社区  ›  AkshayM

动态加载组件中的角度模板字符串

  •  0
  • AkshayM  · 技术社区  · 6 年前

    我想像这样动态加载角度模板:

    import { getHTMLTemplate } from './util';
    
    @Component({
        selector: 'app-button',
        // templateUrl: './button.component.html',
        template: `
            <div>
                some div
            </div>
            ${getHTMLTemplate()}
        `,
        styleUrls: ['./button.component.less'],
    })
    

    getHTMLTemplate 实施:

    export function getHTMLTemplate(){
        return getTemplateWithSomeComplexLogic();
    };
    
    export function getTemplateWithSomeComplexLogic() {
        return '<app-something>dynamic template</app-something>';
    }
    

    这很管用 ng serve ,但失败了 ng build .
    它引发以下错误:

      Error during template compile of 'getHTMLTemplate'
      Function calls are not supported in decorators but 'getTemplateWithSomeComplexLogic' was called.
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Adrian Brand    6 年前

    尝试将函数附加到组件

    getHTMLTemplate = getHTMLTemplate;
    

    getHTMLTemplate = () => getHTMLTemplate();
    

    getHTMLTemplate() {
      return getHTMLTemplate();
    }
    

    useADifferentFunctionName() {
      return getHTMLTemplate();
    }