admin管理员组文章数量:1410725
In my Nuxt project, I created a custom plugin file that returns an object. /helpers/settings
:
export const settings = {
baseURL: '',
...
};
I register this file in /plugins/settings.ts
:
import Vue from 'vue';
import { settings } from '~/helpers/settings';
Vue.prototype.$settings = settings;
And in nuxt.config.js
:
export default {
...
plugins: [
'~/plugins/settings',
Then, in a ponent, I can use my plugin like so:
export default Vue.extend({
data() {
return {
url: `${this.$settings.baseURL}/some-path`,
Everything works as expected, except in that in my console, I get a typescript error from the line that I refer to my plugin in my ponent:
Property '$settings' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<Record<never, any>>>'.
Hence my question: what is the proper way to apply a type to my custom plugin so that I don't get this error every time I use it?
In my Nuxt project, I created a custom plugin file that returns an object. /helpers/settings
:
export const settings = {
baseURL: 'https://my-site.',
...
};
I register this file in /plugins/settings.ts
:
import Vue from 'vue';
import { settings } from '~/helpers/settings';
Vue.prototype.$settings = settings;
And in nuxt.config.js
:
export default {
...
plugins: [
'~/plugins/settings',
Then, in a ponent, I can use my plugin like so:
export default Vue.extend({
data() {
return {
url: `${this.$settings.baseURL}/some-path`,
Everything works as expected, except in that in my console, I get a typescript error from the line that I refer to my plugin in my ponent:
Property '$settings' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<Record<never, any>>>'.
Hence my question: what is the proper way to apply a type to my custom plugin so that I don't get this error every time I use it?
Share Improve this question asked Sep 27, 2019 at 19:09 americanknightamericanknight 7593 gold badges18 silver badges39 bronze badges2 Answers
Reset to default 5According to the docs, you'll need to augment the type file for Vue.
Place the following code in a file named plugin-types.d.ts
.
// 1. Make sure to import 'vue' before declaring augmented types
import Vue from 'vue'
// 2. Specify a file with the types you want to augment
// Vue has the constructor type in types/vue.d.ts
declare module 'vue/types/vue' {
// 3. Declare augmentation for Vue
interface Vue {
$settings: string
}
}
This answer also work but I found an easier, though dirtier, way to fix it without the need of adding the plugin-types.d.ts
file.
Just add a property to you ponen with the name of plugin like following:
@Component
export default class YourComponent extends Vue {
private $settings!: string; // your plugin here
private url: string = `${this.$settings.baseURL}/some-path`
}
本文标签: javascriptTypescripting custom plugins in NuxtStack Overflow
版权声明:本文标题:javascript - Typescripting custom plugins in Nuxt - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744789372a2625193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论