Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Dynamically add css theme when the App.vue has been mounted?

I'm trying to add a CSS theme file globally inside the root component App.vue in a Vue3 project. I have to process it as a component props (I can't change this behavior since the css file can change dynamically and I need to handle this change every time it is mounted again), so the link will be available once the whole app is mounted.

I've been trying to achieve this inside App.vue appending to the header a <link> tag but I'm getting the MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled. error even if I specified the text/css type:

export default defineComponent({
  name: "Application",
  props: {
    themeUrl: {
      type: String,
      required: false,
    },
  },

  data() {
    return {};
  },
  created() {
    if (this.themeUrl) {
      console.log(this.themeUrl);
      let file = document.createElement("link");
      file.rel = "stylesheet";
      file.type = "text/css";
      file.href = this.themeUrl;
      document.head.appendChild(file);
    }
  },
});
</script>

Is there a way to achieve this? I think I can get this theme url even before the app mounting phase but I don't know if and how to tell Vue to use it globally anyway.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you have a direct link to the css file then this should be sufficient, make sure to remove the tag when unmounted if you need to swap the theme. It looks that your file.type = "text/css"; is the cause of the issue.

  mounted () {
    const file = document.createElement('link')
    file.rel = 'stylesheet'
    file.href = 'https://gist.githubusercontent.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css'
    document.head.appendChild(file)
  }

You could also fetch the file content and load it into an existing tag:

  • Fetch the theme file in a lifecycle hook (mounted())
  • Read its content
  • Apply the style with the corresponding querySelector

With this tag in the head :

<style id="customStyle"></style>

And a similar behavior in your component :

export default defineComponent({
  name: "Application",
  props: {
    themeUrl: {
      type: String,
      required: false,
    },
  },

  data() {
    return {};
  },
  mounted() {
    if (this.themeUrl) {
      console.log(this.themeUrl);
      fetch(this.themeUrl)
        .then(response => {
          response.text().then(content => {
            document.getElementById('customStyle').innerHTML = content
          })
        })
    }
  },
});
</script>

https://stackoverflow.com/a/68003923/4390988

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!