javascript - How can I give a dynamic class to vue3 main div #app? - Stack Overflow

I am trying to give a dynamic class to my vue3 app's main div.When I create the app with the mand

I am trying to give a dynamic class to my vue3 app's main div.

When I create the app with the mands down below, it creates a main div tag which has an id app. I can change the styling with css but since I don't have it in the page I can not change class dynamically.

Code to create the app

const app = createApp(App)
app.use(store)
app.use(router)
app.mount('#app')

What I want to do

<div id="app" :class="myClass"></div>

I am trying to give a dynamic class to my vue3 app's main div.

When I create the app with the mands down below, it creates a main div tag which has an id app. I can change the styling with css but since I don't have it in the page I can not change class dynamically.

Code to create the app

const app = createApp(App)
app.use(store)
app.use(router)
app.mount('#app')

What I want to do

<div id="app" :class="myClass"></div>
Share Improve this question edited Nov 12, 2020 at 19:13 Boussadjra Brahim 1 asked Sep 30, 2020 at 21:38 Aiden PearceAiden Pearce 3001 gold badge4 silver badges19 bronze badges 3
  • 1 What's stopping you from doing that? – Dan Knights Commented Sep 30, 2020 at 21:57
  • 1 In Vue3 when you create the app it creates the main <div> tag with app id. But you can not reach it in the code because it is being created in the building process. @Daniel_Knights – Aiden Pearce Commented Oct 1, 2020 at 10:22
  • Oh, I see. Good to know – Dan Knights Commented Oct 1, 2020 at 10:35
Add a ment  | 

2 Answers 2

Reset to default 5

You could add property that represent your class then use watch to update the your root element using document.querySelector('#app').classList = [val], i made the following example that illustrate a real work example which is the mode change from dark to light and vice-versa, i added property called mode and i change it using a button click event then i watch it to update the root element class :

const {
  createApp
} = Vue;
const App = {
  data() {
    return {
      mode: 'is-light'
    }
  },
  watch: {
    mode(val) {
      document.querySelector('#app').classList = [val]
    }
  }
}
const app = createApp(App)
app.mount('#app')
#app {
  padding: 12px;
  border-radius: 4px
}

.is-dark {
  background: #454545;
  color: white
}

.is-light {
  background: #f9f9f9;
  color: #222
}

button {
  -webkit-transition: all .2s ease;
  transition: all .2s ease;
  padding: 10px;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  color: #fff;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  background: transparent;
  background: #4545ee
}
<script src="https://unpkg./[email protected]/dist/vue.global.prod.js"></script>

<div id="app" class="is-dark">

  <button @click="mode= 'is-dark'" class="is-dark">
Dark
</button>

  <button @click="mode= 'is-light'" class="is-light">
Light
</button>
  <p>
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Corrupti quidem aspernatur provident fugit impedit esse itaque iusto iure voluptatem eaque?
  </p>

</div>

Unfortunately, you're not going to be able to do that with Vue directly.

Defining :class="myClass" on root element will not be reactive.

...but you can handle that manually.

you can use this.$el.parentElement in a method (like mounted) and that will allow you to manage the class attribute. (there are several methods like addClass, toggleClass on the classList API)

If you want that to be reactive, you can use a puted or watch to modify that based on some other parameter, but you'd need to provide more context to have more detail on that.

If possible though (depends on the setup/need), you could nest the application in another level of div, so you can control the class.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744069125a4553218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信