有人能解释一下给 vue.js 数据属性指定类型的语法吗(应用是用 typescript 写的)? 我想要的大概是下面这样。
@Component({
data() {
return {
groups: Array<GroupInfo> = []
}
}
})
但 vue 把类型当成了值,让我搞不清该怎么做。
有人能解释一下给 vue.js 数据属性指定类型的语法吗(应用是用 typescript 写的)? 我想要的大概是下面这样。
@Component({
data() {
return {
groups: Array<GroupInfo> = []
}
}
})
但 vue 把类型当成了值,让我搞不清该怎么做。
Can somebody explain the syntax for assigning a type to vue.js data properties (the application uses typescript)? What I'm after looks something like this:
@Component({
data() {
return {
groups: Array<GroupInfo> = []
}
}
})
However, vue treats the type like a value, leaving me confussed about the right approach.
这里不能像平时一样在类型前加 :,因为这会与 JavaScript 对象字面量的 key: value 语法冲突。可以改用类型推断,把最初的空数组 [] 断言为 GroupInfo[] 类型。
@Component({
data() {
return {
groups: [] as GroupInfo[]
}
}
})
Putting : before a type in the usual manner won't work here, since it clashes with the key: value syntax of JavaScript object literals. Instead, rely on type inference, asserting that the initially empty array [] has type GroupInfo[]:
@Component({
data() {
return {
groups: [] as GroupInfo[]
}
}
})
可以考虑通过 vue-class-component 使用类风格组件 (https://v2.vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Component%E2%80%8C%E2%80%8Bs),这样就能把这些声明移到类属性里,而且与 Typescript 配合得很好。
import Vue from 'vue'
import Component from 'vue-class-component'
// @Component marks this class as a Vue component
@Component({
// Component options can all be supplied here
template: '<button @click="notify">Click!</button>'
})
export default class NoticeComponent extends Vue {
// Instance properties can hold the initial data
greeting: string = 'Hello!'
// Instance methods can provide component methods
notify (): void {
window.alert(this.greeting)
}
}
Consider class-style components (https://v2.vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Component%E2%80%8C%E2%80%8Bs) through vue-class-component, which let those declarations become properties of the class and work nicely with Typescript.
import Vue from 'vue'
import Component from 'vue-class-component'
// @Component marks this class as a Vue component
@Component({
// Component options can all be supplied here
template: '<button @click="notify">Click!</button>'
})
export default class NoticeComponent extends Vue {
// Instance properties can hold the initial data
greeting: string = 'Hello!'
// Instance methods can provide component methods
notify (): void {
window.alert(this.greeting)
}
}
我通过 Java Mail API 发送消息。邮件到达后,收件箱显示的是发件地址。我想用一些文本替代这个地址。这应该是可以的,因为我收到的 facebook 邮件在收件箱里显示 "Facebook - Message title",而不是 "mail…
我的 CRM 代码在网页资源中运行,所以要获取实体数据,就得写成 这样来读取数据。 可惜用 typescript 编写时,这个表达式无法编译。现在我手动把 window.parent 加到 Xrm.Page 前面……(Xrm 类型定义已经装好了),这…
我想发送一个 Date 响应头。所需的格式如下。 这是我目前尝试的代码。