VUE.jsのVue
export default
内で構文は単一のコンポーネントをエクスポートするために使用されます。 メソッドは、コンポーネントの構成内のexport default
オブジェクトプロパティ内で定義されます。このオブジェクトは、コンポーネントに持ちたいすべての方法のコンテナとして機能します。 各メソッドはキー値のペアであり、キーはメソッドの名前(それを呼び出すために使用)であり、値はメソッドの関数定義です。methods
export default { name: 'MyComponent', data() { return { message: 'Hello, Vue!' }; }, methods: { greet() { console.log(this.message); }, anotherMethod(param) { console.log('Another method called with parameter:', param); } } };
およびgreet
はanotherMethod
オブジェクト内で定義されています。 メソッド内の内部とは、VUEコンポーネントインスタンスを指し、データやその他のコンポーネントプロパティへのアクセスを許可します。methods
this
エクスポートされたデフォルトコンポーネント内で定義されたメソッドへのアクセス
ディレクティブ(またはexport default
> shorthand)を使用してメソッドを直接呼び出すことができます:export default
v-on
@
>
<template> <div> <button @click="greet">Greet</button> <button @click="anotherMethod('Hello')">Another Method</button> </div> </template> <script> import MyComponent from './MyComponent.vue'; export default { components: { MyComponent } }; </script>
import MyComponent from './MyComponent.vue'; const componentInstance = new MyComponent(); componentInstance.greet(); // Calls the greet method componentInstance.anotherMethod('World'); // Calls anotherMethod
export default
以上がVueでコンポーネントのメソッドを構成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。