【问题标题】:Vue.js - Prism code is not shown from a computed propertyVue.js - 棱镜代码未从计算属性中显示
【发布时间】:2020-06-14 12:07:13
【问题描述】:

出于学习 Vue 的目的,我正在尝试创建一个非常简单的应用程序,它将实时从输入中返回突出显示的代码。我浏览了一些 Prism 教程和示例,但无法使其正常工作。任何帮助或指导都将不胜感激,因为我刚刚开始使用 Vue,我感觉我在混淆一些东西。

这是HelloWorld.vue

<template>
  <div>
    <h1>Prism Demo</h1>
    <div id="editor">
      <textarea v-model="message"></textarea>
      <div>{{ highlighteddMessage }}</div>
    </div>
  </div>
</template>

<script>
import Prism from 'vue-prism-component'
export default {
  data() {
    return {
      message: `var myFunction = function() {
    statements
}`
};
  },
      computed: {
        highlighteddMessage: function () {
        return Prism.highlight(this.message, Prism.languages.js);
      }
    }
}
</script>

<style scoped>
...
</style>

还有我的main.js

import Vue from 'vue'
import App from './App.vue'

import "prismjs";
import "prismjs/themes/prism-funky.css";
import "prismjs/components/prism-scss.min";
import "prismjs/plugins/autolinker/prism-autolinker.min";
import "prismjs/plugins/autolinker/prism-autolinker.css";
import Prism from "vue-prism-component";
Vue.component("prism", Prism);

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

我认为问题在于我如何尝试在计算属性中使用 Prism,但我无法修复它。将感谢有关在 Vue 中正确使用 Prism 的任何提示。

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    您应该将Prism 添加到您的组件选项components:{Prism},然后在模板中使用该组件包装代码,无需创建计算属性:

      <div>
        <h1>Prism Demo</h1>
        <div id="editor">
          <textarea v-model="message"></textarea>
    
    
      <prism language="javascript">{{ message  }}</prism>
        </div>
      </div>
    </template>
    
    <script>
    import Prism from 'vue-prism-component'
    export default {
      data() {
        return {
          message: `var myFunction = function() {
        statements
    }`
    };
      },
      components:{
         Prism
       }
    }
    </script>
    

    【讨论】:

    • 谢谢!如果我想使用 Excel 插件而不是 js,你知道如何包含它吗?我尝试了以下方法,但没有运气:github.com/mAAdhaTTah/babel-plugin-prismjs
    • 你想高亮excel数据吗?
    • 我想用的就是这个:Excel Formula - excel-formula, xlsx, xlsprismjs.com
    • 你试过了吗` {{ message }}`
    • 是的,但出现此错误:vue.runtime.esm.js?2b0e:1888 Error: Prism component for language "xls" was not found, did you forget to register it? See all available ones: https://cdn.jsdelivr.net/npm/prismjs/components/
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 2017-07-29
    • 2019-03-18
    • 2017-08-26
    • 1970-01-01
    • 2016-04-04
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多