NuxtJS Typescript click.native '无法读取 null 的属性 'test'



我在nuxtjs中创建了一个自定义导航栏,如果我想在单击n链接时关闭导航栏,则会收到此错误:无法读取null的属性"test"。 我正在使用nuxt-property-decorator依赖项。

我的代码:

<template>
<!-- MOBILE NAVBAR -->
<nav class="navbar">
<mq-layout mq="sm" class="navbar-wrapper" :class="$mq">
<button class="navbar-dropdown-button-mobile" @click="this.toggleDropdownMenu"> <!-- set dropdown enabled / disabled -->
<picture>
<img
class="navbar-dropdown-img-mobile"
src="@/assets/icons/dropdown_menu_button.png"
alt="Menu"
/>
</picture>
</button>
</mq-layout>
<!-- ... Other Code -->
<!-- DROPDOWN -->
<mq-layout mq="sm" class="navbar-dropdown-menu-mobile" v-if="this.showDropdownMenu"> <!-- show/hide dropdown depending on showDropDownMenu boolean  -->
<n-link class="navbar-link-mobile" :class="$mq" to="/" prefetch>LINK</n-link>
<n-link class="navbar-link-mobile" :class="$mq" @click.native="this.test" to="/pricing" prefetch>LINK</n-link> <!-- This @click.native thorws this error -->
<n-link class="navbar-link-mobile" :class="$mq" to="/documentation" prefetch>LINK</n-link>
<n-link class="navbar-link-mobile" :class="$mq" to="/support" prefetch>LINK</n-link>
</mq-layout>
<!-- ... Other Code -->
</nav>
</template>
<script lang="ts">
import Vue from 'vue';
import { Component } from 'nuxt-property-decorator';
@Component({})
export default class NavbarComponent extends Vue {
protected showDropdownMenu: boolean = false;
protected toggleDropdownMenu() {
this.showDropdownMenu = !this.showDropdownMenu;
}
test() {
console.log("HIIIIII")
}
}
</script>

不要在 Vue 文件的模板中使用this

<n-link class="navbar-link-mobile" :class="$mq" @click.native="test" to="/pricing" prefetch>LINK</n-link>

https://v2.vuejs.org/v2/guide/events.html

最新更新