I know that there is another question with this topic, but it does not use import syntax and nuxt-js. In my nuxt app, I have four themes that the user could change them with a button on navigation. Each time the user change theme, a state in my store that is called themeName is changed. In one of my pages I get the value of that state and store it in a computed property called themeNameActive as follow:
themeNameActive: function () {
return this.$store.state.setActive.themeName;
},
Then I set a watch
to that computed property as follow to monitor the changes in my themes:
themeNameActive: function () {
this.asyncImport();
}
The asyncImport
is a method that must change the highlight.js themes according to the theme that was selected by user as the below code:
asyncImport() method:
asyncImport: async function () {
console.log(this.themeNameActive);
let themeObj = {
default: "github",
kind1: "idea",
kind2: "tomorrow-night-bright",
kind3: "default"
}
await import('highlight.js/styles/' + themeObj[this.themeNameActive] + '.css');
hljs.highlightAll();
}
Here, default, kind1, kind2 and kind3 are the supported values that this.themeNameActive
computed property could accept and are changing with user selection. This strategy works fine and the highlight of the <code>
tag in my page is changed with the change of the theme. But the problem is that when the user selects for example kind1
theme and after that selects another one (for example kind2
) and finally returns to kind1
again, the kind2
theme remains and kind1
does not applied. I think the reason of that problem is CSS overwrites styles of last selected theme and don't import again the previous selections. So is there any solution that clear or removes previous imports to overcome this problem. I am not very familiar with es6
modules and import
command capabilities. Also I search for a solution that don't use sass or needs to reload the entire page.