【发布时间】:2021-02-24 20:54:45
【问题描述】:
我刚刚使用 Typescript 设置了一个新的 Nuxt.js 应用程序,但我无法解决一些似乎与 $auth 模块相关的 Typescript 错误。
这是我tsconfig.json 中的“类型”设置:
"types": [
"@types/node",
"@nuxt/types",
"@types/nuxtjs__auth",
"@nuxtjs/axios",
]
代码如下:
async login() {
try {
await this.$auth.loginWith('local', {
data: {
user: {
email: this.email,
password: this.password
}
}
})
this.$router.push('/dashboard')
} catch (e) {
this.error = e.response.data.message
}
}
这是错误:
Property '$auth' does not exist on type '{ login(): Promise<void>; }'.
我发现了一些关于将类型添加到 tsconfig.json 的 SO 帖子,我做到了,但它并没有解决这个问题。
我该如何解决这个问题?
编辑:
nuxt.config.js
require('dotenv').config()
const axiosUrl = process.env.AXIOS_BASE_URL;
const port = process.env.NODE_ENV === 'production' ? process.env.PORT : 3333;
export default {
// Disable server-side rendering (https://go.nuxtjs.dev/ssr-mode)
ssr: true,
// Global page headers (https://go.nuxtjs.dev/config-head)
head: {
title: 'ollie-frontend',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
// Global CSS (https://go.nuxtjs.dev/config-css)
css: [
],
// Plugins to run before rendering page (https://go.nuxtjs.dev/config-plugins)
plugins: [
],
// Auto import components (https://go.nuxtjs.dev/config-components)
components: true,
// Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
buildModules: [
// https://go.nuxtjs.dev/tailwindcss
'@nuxtjs/tailwindcss',
'@nuxtjs/google-fonts',
['@nuxt/typescript-build', {
typeCheck: true,
ignoreNotFoundWarnings: true,
}],
],
// Modules (https://go.nuxtjs.dev/config-modules)
modules: [
'@nuxtjs/axios',
'@nuxtjs/auth'
],
// Build Configuration (https://go.nuxtjs.dev/config-build)
build: {
},
server: {
port: port,
},
axios: {
baseURL: axiosUrl
},
auth: {
redirect: {
login: '/sign_in',
logout: '/',
},
strategies: {
local: {
endpoints: {
login: { url: '/login', method: 'post', propertyName: 'token' },
register: { url: '/signup', method: 'post' },
user: { url: '/api/v1/users/me', method: 'get', propertyName: 'user' },
logout: false
}
}
}
},
router: {
middleware: ['auth']
},
typescript: {
typeCheck: {
eslint: {
files: './**/*.{ts,js,vue}'
}
}
},
}
tsconfig.json:
{
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"moduleResolution": "Node",
"lib": [
"ESNext",
"ESNext.AsyncIterable",
"DOM"
],
"esModuleInterop": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/types",
"@types/nuxtjs__auth",
"@nuxtjs/axios",
]
},
"exclude": [
"node_modules"
]
}
【问题讨论】:
-
请在 tsconfig 中分享您的
nuxt.config.js文件和compilerOptions -
你使用的是
export default Vue.extend({ ... })还是export default { ... }? -
不,
nuxt.conf.js和tsconfig.js文件的内容 -
@BoussadjraBrahim 我已将其添加到帖子中
-
@NinoFiliu export default { } only,不是 Vue.extend
标签: typescript vue.js nuxt.js