1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nuxt.jsでメソッドをグローバルで使用できるようにする方法

Posted at

やりたいこと

バックエンドからユーザーネームを取得するのだが、カラムがfirst_name,last_nameに別れている。
どのコンポーネントからでも、$userFullNameを使用すれば、フルネームを取得できるようにしたい!!

手順1、 pluginにutils.jsを作成する。

※util は ユーティリティのことで、英語で「役に立つもの」という意味
ファイル名は何でも大丈夫です!

const userFullName = (profile) => {
  return profile.lastName + ' ' + profile.firstName
}

export default ({_}, inject) => {
  inject('userFullName', userFullName);
}

手順2、 nuxt_confingにpluginを記載!

  plugins: [
    { src: '@/plugins/utils' }
  ],

手順3, コンポーネントやページから関数を呼び出す!

{{ $userFullName(profile) }}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?