最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js 2, Using custom component tag names in css? - Stack Overflow

programmeradmin1浏览0评论
<template>
    <header>
        <hamburger></hamburger>
        <app-title></app-title>
        <lives></lives>
    </header>
</template>

<script>
export default {
    name: 'Titlebar',
    data() {
        return {

        }
    }
}
</script>

<style lang="scss" scoped>
    @import "../styles/variables";

    header {
        padding: $padding-titlebar;
        position: relative;
    }
    lives {
        position: absolute;
        right: 2.5vh;
    }
</style>

Is it possible to use ponent tags like any regular HTML tag for styling purposes like I've written down there in lives { }?
I see that that writing <lives class="lives"> and using .lives { } in css works but that seems kinda redundant, would rather like to ommit adding aditional classes if it's possible to just use ponent tag.
I understand that Vue piles <lives> into HTML code and that there is no "lives" tag for css to use after it's piled, but still wondering.

<template>
    <header>
        <hamburger></hamburger>
        <app-title></app-title>
        <lives></lives>
    </header>
</template>

<script>
export default {
    name: 'Titlebar',
    data() {
        return {

        }
    }
}
</script>

<style lang="scss" scoped>
    @import "../styles/variables";

    header {
        padding: $padding-titlebar;
        position: relative;
    }
    lives {
        position: absolute;
        right: 2.5vh;
    }
</style>

Is it possible to use ponent tags like any regular HTML tag for styling purposes like I've written down there in lives { }?
I see that that writing <lives class="lives"> and using .lives { } in css works but that seems kinda redundant, would rather like to ommit adding aditional classes if it's possible to just use ponent tag.
I understand that Vue piles <lives> into HTML code and that there is no "lives" tag for css to use after it's piled, but still wondering.

Share Improve this question asked Aug 15, 2017 at 9:50 Miloš TomšikMiloš Tomšik 2251 gold badge3 silver badges9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

Vue ponent's name has no relationship with css rule, but it's template does.

Vue ponent has a template property, it contains html tag. and also, you can use custom html tag in template. for example:

template: `<lives class="lives">{{a}}</lives>`

so now you can define css rule by lives tag.

You can use vue-custom-element:

https://github./karol-f/vue-custom-element

First register your ponent with:

Vue.customElement('widget-vue', {
  props: [
    'prop1',
    'prop2',
    'prop3'
  ],
  data: {
    message: 'Hello Vue!'
  },
  template: '<p>{{ message }}, {{ prop1  }}, {{prop2}}, {{prop3}}</p>'
});

Then you can do:

widget-vue {
  display: block;
  background: red;
  ...
}

If you want to register a Single File Component, register your own like this:

import TopBar from '@/ponents/core/TopBar'
Vue.customElement('top-bar', TopBar)

I hope it helps, Cheers!

发布评论

评论列表(0)

  1. 暂无评论