Vus Js – How to show javascript code with opening and closing script tag as v-html or in textarea input box

huangapple go评论114阅读模式
英文:

Vus Js - How to show javascript code with opening and closing script tag as v-html or in textarea input box

问题

在我的vue.js应用程序中,我有这段HTML代码:

<b-row>
    <b-col md="12">
        <h5>在每个页面的头部添加以下代码</h5>
        <b-form-textarea readonly name="header_code" id="textarea" v-model="header_code" rows="10" max-rows="6"></b-form-textarea>
    </b-col>
    <b-col md="12" class="mt-1">
        <h5>在感谢页面上添加以下代码,用实际数据替换演示数据</h5>
        <b-form-textarea readonly name="thank_you" id="textarea" v-model="thank_you" rows="10" max-rows="6"></b-form-textarea>
    </b-col>
</b-row>

在这里,我想在thank_youv-model上设置JavaScript代码以及开头和结尾的<script>标签,以便用户可以复制代码。

我正在这样做:

created() {
    this.callThankYouCode();
},
methods: {
    callThankYouCode() {
        // ...(你的代码)
    },
}

但是,如果我添加开头和结尾的<script>标签,应用程序就无法正常工作,并在控制台日志中看到这种错误:

[vue-router] Failed to resolve async component default: Error: Module
build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError:
/Applications/MAMP/htdocs/FeedParserProject/resources/js/src/views/apps/poas/settings.vue:
Unterminated template. (456:32)

454 | 'purchase', 455 | {
> 456 | token: '${this.jsToken}',
| ^ 457 | items: [ 458 | 459 | {item_id: "id1", quantity: X1 (numeric),
price_per_item_without_vat: Y1 (numeric)},

所以,你可以告诉我如何在thank_you数据属性中使用JavaScript代码以及开头和结尾的<script>标签,并在HTML区域中使用它吗?

英文:

In my vus js application, I have this html code:

&lt;b-row&gt;
    &lt;b-col md=&quot;12&quot;&gt;
        &lt;h5&gt;Add the following code in the header of every page&lt;/h5&gt;
        &lt;b-form-textarea readonly name=&quot;header_code&quot; id=&quot;textarea&quot; v-model=&quot;header_code&quot; rows=&quot;10&quot; max-rows=&quot;6&quot;&gt;&lt;/b-form-textarea&gt;
    &lt;/b-col&gt;
    &lt;b-col md=&quot;12&quot; class=&quot;mt-1&quot;&gt;
        &lt;h5&gt;Add the following code on the thank you page, replace the demo data with live data&lt;/h5&gt;
        &lt;b-form-textarea readonly name=&quot;thank_you&quot; id=&quot;textarea&quot; v-model=&quot;thank_you&quot; rows=&quot;10&quot; max-rows=&quot;6&quot;&gt;&lt;/b-form-textarea&gt;
    &lt;/b-col&gt;
&lt;/b-row&gt;

Here, on this thank_you v-model I want set the javascript code along with opening and closing script tag so that user can copy the code.

What I am doing this :

created() {
    callThankYouCode
},
methods: {
    callThankYouCode() {
        const jsToken = this.jsToken;
        const code = `tsTracker(
&#39;track&#39;,
&#39;purchase&#39;,
{
    token:  &#39;${this.jsToken}&#39;,
    items: [

            {item_id: &quot;id1&quot;, quantity: X1 (numeric), price_per_item_without_vat: Y1 (numeric)},
            {item_id: &quot;id2&quot;, quantity: X2 (numeric), price_per_item_without_vat: Y2 (numeric)},
            {item_id: &quot;id3&quot;, quantity: X3 (numeric), price_per_item_without_vat: Y3 (numeric)}
    ],
    // Price without VAT = Price with VAT / (1 + VAT rate)
    order_total_without_vat: TOTAL_CART (numeric), // without shipping costs or applied discounts, must be numeric
    order_shipping_total_without_vat: TOTAL_SHIPPING (numeric), // shipping costs without VAT
    user_data: {
        // this is optional, but the data is usefull it you are planning to use Facebook Conversion API for a better matchig, it is recommended if you want to you POAS in Facebook, for GDPR you must add it in TOS
        email: &quot;&quot;,
        phone: &quot;&quot;,
        last_name: &#39;&#39;,
        first_name: &#39;&#39;
    },
    currency: &#39;RON&#39;,
    transaction_id: &#39;ORDER_ID_OR_REFERENCE&#39;,
    other_costs: 0, // if there are any other costs, for example packing price etc, without VAT
    order_date: &#39;ORDER Placement date&#39;, // it should be exactly how its inserted in the database, not autogenerated with javascript, using the format Y-m-d H:i:s
}
);
        `;

        this.thank_you = code;
    },
}

On created hook I called this method callThankYouCode. On this method, I have set the thank_you data property value. It's working fine.

but

I want to add the opening and closing script tag on this code variable. If I do so then the application is not working. I see this type of error on the console log:

> [vue-router] Failed to resolve async component default: Error: Module
> build failed (from ./node_modules/babel-loader/lib/index.js):
> SyntaxError:
> /Applications/MAMP/htdocs/FeedParserProject/resources/js/src/views/apps/poas/settings.vue:
> Unterminated template. (456:32)
>
> 454 | 'purchase', 455 | {
> > 456 | token: '${this.jsToken}',
> | ^ 457 | items: [ 458 | 459 | {item_id: "id1", quantity: X1 (numeric),
> price_per_item_without_vat: Y1 (numeric)},

So, can you tell me how can I use a this javascript code with opening and closing script tag to the thank_you data propery and use it in the html area?

答案1

得分: 1

I've found a solution to display HTML code as plain text in a Vue.js template. You can achieve this by using the v-html directive Here's an example implementation:

<!-- language: lang-html -->

&lt;b-form-textarea readonly name=&quot;thank_you&quot; id=&quot;textarea&quot; v-html=&quot;thank_you&quot; rows=&quot;20&quot; max-rows=&quot;10&quot;&gt;&lt;/b-form-textarea&gt;

<!-- end snippet -->

In the above code snippet, I bind the thank_you variable to the v-html directive, which tells Vue.js to interpret the content as HTML. the content is displayed as preformatted text, maintaining the formatting and rendering the code as plain text.

To incorporate the <script> tags at the beginning and end of the code, you can make the following adjustment in your Vue component's data section:

<!-- language: lang-js -->

        this.thank_you = &quot;&amp;lt;script&amp;gt;&quot; + code + &quot;&amp;lt;/script&amp;gt;&quot;;

<!-- end snippet -->

Please remember to replace code with your actual code string.

Following these changes, the code will be rendered as plain text within the template, including the <script> tags, without causing conflicts with the Vue.js template syntax.

Ensure that you properly escape any HTML entities or special characters in the code to ensure correct rendering and prevent any potential security vulnerabilities.

英文:

I've found a solution to display HTML code as plain text in a Vue.js template. You can achieve this by using the v-html directive Here's an example implementation:

<!-- language: lang-html -->

&lt;b-form-textarea readonly name=&quot;thank_you&quot; id=&quot;textarea&quot; v-html=&quot;thank_you&quot; rows=&quot;20&quot; max-rows=&quot;10&quot;&gt;&lt;/b-form-textarea&gt;

<!-- end snippet -->

In the above code snippet, I bind the thank_you variable to the v-html directive, which tells Vue.js to interpret the content as HTML. the content is displayed as preformatted text, maintaining the formatting and rendering the code as plain text.

To incorporate the <script> tags at the beginning and end of the code, you can make the following adjustment in your Vue component's data section:

<!-- language: lang-js -->

        this.thank_you = &quot;&amp;lt;script&amp;gt;&quot; + code + &quot;&amp;lt;/script&amp;gt;&quot;;

<!-- end snippet -->

Please remember to replace code with your actual code string.

Following these changes, the code will be rendered as plain text within the template, including the <script> tags, without causing conflicts with the Vue.js template syntax.

Ensure that you properly escape any HTML entities or special characters in the code to ensure correct rendering and prevent any potential security vulnerabilities.

huangapple
  • 本文由 发表于 2023年6月8日 00:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定