Vue文件中初始化带有继承关系的类型时,无法初始化子类变量
Vue version
3.3.4
Link to minimal reproduction
https://github.com/binyet/vitetsdemo
Steps to reproduce
在src/services中有一个GetLogModel.ts文件,其中定义了GetLogModel类,其继承于GetPaginationModel,当在HelloWorld.vue进行new GetLogModel时,打印的结果只有pageIndex和pageSize有值,其他属性都为undefined,无论是否赋值。 但是,如果把GetLogModel.ts中的类型全部复制到HelloWorld.vue中,再进行new GetLogModel(),就会正常的将其属性赋上值。 此外,如果抛开Vue,只运行ts代码时,也会正常赋值。
What is expected?
在new GetLogModel时,根据传入的值,进行子类属性赋值
What is actually happening?
在new GetLogModel时 只对父类的属性进行了赋值。
System Info
No response
Any additional comments?
No response
I have a try in TypeScript playground and Vue playground the result are the same. @binyet
TypeScript playgound:
Vue playground:
首先感谢您的回复,我也在Vue playground试过,结果确实如此。 但是在vsCode中运行后,打开浏览器的console却是以下结果
I have a try, your tsconfig.json is have set the useDefineForClassFields
true
, so it has this behavior.
Solution:
{
"compilerOptions": {
"target": "ES2020",
// "useDefineForClassFields": true, // Comment this line in the code and have a try
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
Next time please first comment some line in the tsconfig.json
and have a try yourself thanks.
https://www.typescriptlang.org/tsconfig#useDefineForClassFields