3
1

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 5 years have passed since last update.

VueのFunctional Componentsに親で付与したCSSを読み込む方法

Posted at

以下のように Vue の Functional なコンポーネントを親から呼び出します。

AppButton.vue
<template functional>
  <button class="app-button">
    <slot />
  </button>
</template>

<style lang="scss" scoped>
.app-button {
  background: red;
}
</style>
親コンポーネント
<template>
  <AppButton class="button-test">ボタン</AppButton>
</template>

<script>
import AppButton from '~/components/AppButton'

export default {
  components: {
    AppButton
  },
}
</script>

<style lang="scss" scoped>
.button-test {
  margin: 10px;
}
</style>

このとき、親で定義した.button-test読み込まれません

詳しくは vue.js: functional componentとscoped css - ぬまのそこ あたりの記事が参考になるのですが、執筆時点ではこの記事の対処法は動作しませんでした。

そこでもう少し調べると、以下のように書けば良いことが分かりました。

AppButton.vue
<template functional>
  <button class="app-button" :class="data.staticClass">
    <slot />
  </button>
</template>

<style lang="scss" scoped>
.app-button {
  background: red;
}
</style>

これで親で定義した class が読み込まれたかと思います。以下の issue に感謝。

"class" and "style" attributes have no effect on functional SFC components · Issue #7985 · vuejs/vue

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?