Angular 模板中的 2 个变量未定义
Angular 2 variable in template is undefined
我正在我的组件中声明一个变量。
例如
let link = 'https://google.com'
但是如果我在我的模板中使用它,它不存在
例如
{{ link }} -> has no output
href="#{{link}}" -> resolved into href="#"
[attr.href]="link" -> resolves into nothing
[attr.href]="link+'#'" -> resolves into href="undefined/#"
我做错了什么?
感谢您的帮助
改变
let link = 'https://google.com'
至
link:string = 'https://google.com';
这段代码
let link = 'https://google.com'
是一个语句,只能在构造函数或方法或函数中使用,但不能在只允许声明的 class' 顶层使用。
应该是
link = 'https://google.com'
代替 let
let 仅用于块范围,要在模板中使用该变量,您必须声明该变量,
PS: use let
only to use wihtin the method/function
有关 let
的详细信息,请参阅此处
我正在我的组件中声明一个变量。
例如
let link = 'https://google.com'
但是如果我在我的模板中使用它,它不存在
例如
{{ link }} -> has no output
href="#{{link}}" -> resolved into href="#"
[attr.href]="link" -> resolves into nothing
[attr.href]="link+'#'" -> resolves into href="undefined/#"
我做错了什么?
感谢您的帮助
改变
let link = 'https://google.com'
至
link:string = 'https://google.com';
这段代码
let link = 'https://google.com'
是一个语句,只能在构造函数或方法或函数中使用,但不能在只允许声明的 class' 顶层使用。
应该是
link = 'https://google.com'
代替 let
let 仅用于块范围,要在模板中使用该变量,您必须声明该变量,
PS: use
let
only to use wihtin the method/function
有关 let
的详细信息,请参阅此处