Ant*_*rka 2 typescript vue.js vue-router
我在使用 TypeScript 的 VueJS 项目中遇到错误,其中我的 vue 文件被分割为 HTML、CSS、TS 和 vue。
我有这个错误:类型 '{ validate(): void; 上不存在属性 '$router' }'
这是我的文件的分割方式:
登录.vue:
<template src="./Login.html"></template>
<script src="./Login.ts"></script>
<style src="./Login.css"></style>
Run Code Online (Sandbox Code Playgroud)
登录.html:
<div id="form" align="center" justify="center">
<v-col sm="4" align="center" justify="center">
<v-form ref="form" v-model="valid" lazy-validation>
<v-text-field v-model="login" :label="$t('Username')" required></v-text-field>
<v-text-field v-model="password" :label="$t('Password')" type="password" required></v-text-field>
<v-btn color=primary class="mr-4" @click="validate">
{{ $t('Login') }}
</v-btn>
</v-form>
</v-col>
</div>
Run Code Online (Sandbox Code Playgroud)
登录.ts:
export default {
name: 'Login',
data() {
return {
fields: {
login: '',
password: ''
}
}
},
methods: {
validate() {
if ("admin" === this.login && "admin" === this.password) {
this.$router.push('index')
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,由于此错误,我无法构建我的应用程序。
有人有什么想法吗?
谢谢 !
安托万
要获得类型推断,您应该使用以下命令创建组件Vue.extend({}):
登录.ts
import Vue from "vue"
export default Vue.extend({
name: 'Login',
data() {
return {
fields: {
login: '',
password: ''
}
}
},
methods: {
validate() {
if ("admin" === this.fields.login && "admin" === this.fields.password) {
this.$router.push('index')
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1017 次 |
| 最近记录: |