Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

还未被加载的动态组件,其样式却被提前被注入到页面的head内了 #2018

Open
ETTTTT opened this issue Nov 21, 2022 · 3 comments

Comments

@ETTTTT
Copy link

ETTTTT commented Nov 21, 2022

// tab1组件
<template>
    <div class="tab">我是tab1组件</div>
</template>
<style >
.tab {
    font-size: 30px;
}
</style>
// tab2组件
<template>
    <div class="tab">我是tab2组件</div>
</template>
<style >
.tab {
    color: red;
}
</style>

// 入口组件

<template>
    <div id="app">
       <div class="box">
        <div @click="tab('tabOne')" class="btn">切换tab1</div>
        <div @click="tab('tabTwo')" class="btn">切换tab2</div>
       </div>
        <component v-bind:is="currentTabComponent"></component>
    </div>
</template>
<script>
import tabOne from './tab1.vue'
// tab2组件一开始还未被使用,但是组件内的样式却已经被注入到head内
import tabTwo from './tab2.vue'

export default {
    components: {
        tabTwo,
        tabOne,
    },
    data() {
        return {
            currentTabComponent: 'tabOne'
        }
    },
    methods: {
        tab(val) {
            this.currentTabComponent = val
        }
    }
}
</script>
<style>
.box{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}
.btn{
    font-size: 20px;
    text-align: center;
    flex: 1;
    background: #ccc;
    margin: 10px;
 }
</style>

@ETTTTT
Copy link
Author

ETTTTT commented Nov 21, 2022

@ETTTTT
Copy link
Author

ETTTTT commented Nov 21, 2022

使用webpack启动的项目不会有这个问题,
使用vue-cli创建的项目,启动会有这个问题

@ETTTTT
Copy link
Author

ETTTTT commented Nov 23, 2022

组件没有被加载之前,组件的样式被提前注入到head内了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant