首頁  >  問答  >  主體

標題重寫為:Vue中新增內聯樣式的CSS變量

我在元件中使用 css 變量,並根據 setup() 中計算的資料設定 div 的寬度

setup(props) {
    const progressBar = PositionService.getProgressBar(props.position);
    const progressWidth = `${progressBar}%`;

    ...
    return { ..., progressWidth };
}

然後我使用這個變數作為 css 變數。

<style lang="scss" scoped>
.progress-bar-width {
  --progress-bar-width: v-bind("progressWidth");
  width: var(--progress-bar-width);
}
</style>

渲染頁面時,我注意到內聯樣式被添加到 html 父元件中,導致

<a href="#/1070/applications/status/1" class="card position-relative border-gray-300 border-hover overflow-hidden h-500px" data-v-61475b35="" style="--61475b35-progressWidth:43.0613%;">.....</a>

CSP 阻止內聯樣式,因此此方法不起作用。 如何在沒有內聯樣式的情況下使用 css 變數?

P粉421119778P粉421119778293 天前473

全部回覆(1)我來回復

  • P粉576184933

    P粉5761849332024-01-01 11:16:14

    這是一個駭客解決方案,但由於沒有內聯樣式,這是我唯一能想到的:

    為您的模板新增「樣式」元件。這將會被 DOM 中的