2017-10-18 5 views
0

私はnuxt jsを使用しています。私は単純なTest()メソッドを持つpluginsフォルダ内にhelper.jsスクリプトを持っています。今、どのように私はこのTest()メソッドをページから使用できますか?nuxt jsを使用してプラグインとしてjsを使用する方法

helper.js

export default function Test() { 
    return 'This is test' 
} 

答えて

1

あなたは自分のコンポーネント(ページ)内のコードを使用したい場合は、あなただけのメソッドをインポートして使用する必要があります。

TestPage.vue

<template> 
    <div> 
    <h1>{{ getTest }}</h1> 
    </div> 
</template> 

<script> 
import test from '~/plugins/helper.js' 

export default { 
    computed: { 
    getTest() { 
     return test() 
    } 
    } 
} 
</script> 
+0

各ページにインポートするのではなく、グローバルにインポートする方法。 –

+0

[vuejs plugins](https://vuejs.org/v2/guide/plugins.html)を使用してください。 – dahrens

関連する問題