3

In VueJS, is there a way to interpolate a string within a string, either in the template or in the script? For example, I want the following to display 1 + 1 = 2 instead of 1 + 1 = {{ 1 + 1 }}.

<template>
    {{ myVar }}
</template>

<script>
    export default {
        data() {
            "myVar": "1 + 1 = {{ 1 + 1 }}"
        }
    }
</script>

Edit: to better illustrate why I would need this, here's what my actual data looks like:

section: 0,
sections: [
    {
        inputs: {
            user: {
                first_name: {
                    label: "First Name",
                    type: "text",
                    val: ""
                },
                ...
            },
            ...
        },
        questions:  [
            ...
            "Nice to meet you, {{ this.section.inputs.user.first_name.val }}. Are you ...",
            ...
        ]
    },
    ...
],

this.section.inputs.user.first_name.val will be defined by the user. While I could rebuild the question properties as computed properties, I would rather keep the existing data structure in tact.

5
  • "myVar": "1 + 1 = "+(1+1)? But why? You might be approaching it the wrong way, tell us the actual thing you are trying to achieve, there might be a better way. Commented Aug 28, 2018 at 16:44
  • I didn't explain the situation well. I have a complex data structure in which variables are built by referencing other variables, such as a sentence containing a user's name. I'm trying avoid a situation where I have to hard-code each sentence in the template, e.g. <p>Hello, {{ user.first_name }}</p> and am looking for a solution that would allow this instead: <p>{{ sentence }}</p>, where sentence = Hello, {{ this.user.first_name }}. Commented Aug 28, 2018 at 16:57
  • Cool, The right way to do this kind of thing would be to use computed properties Commented Aug 28, 2018 at 17:01
  • Thanks. That would work for me normally, but in my particular case, I need to first evaluate strings that refer to child variables before computing parent variables. I posted a solution that worked for me. Commented Aug 28, 2018 at 18:00
  • "child variables before computing parent variables" - Can you explain this further? I'm not able to understand what is parent and child here. Commented Aug 28, 2018 at 18:14

1 Answer 1

3

I found the solution I was looking for from https://forum.vuejs.org/t/evaluate-string-as-vuejs-on-vuejs2-x/20392/2, which provides a working example on JsFiddle: https://jsfiddle.net/cpfarher/97tLLq07/3/

<template>
    <div id="vue">
        <div>
            {{parse(string)}}
        </div>
    </div>
</template>

<script>
    new Vue({
        el:'#vue',
        data:{
            greeting:'Hello',
            name:'Vue',
            string:'{{greeting+1}} {{name}}! {{1 + 1}}'
        },
        methods:{
            evalInContext(string){
                try{
                    return eval('this.'+string)
                } catch(error) {
                    try {
                        return eval(string)
                    } catch(errorWithoutThis) {
                        console.warn('Error en script: ' + string, errorWithoutThis)
                        return null
                    }
                }
            },
            parse(string){
                return string.replace(/{{.*?}}/g, match => {
                    var expression = match.slice(2, -2)
                    return this.evalInContext(expression)
                })
            }
        }
    })
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think it will support string:'{{greeting+name}}
Can you provide your actual use case as I'm interested in knowing where computed property fails short? In the case shown here, a simple computed property seems to be perfectly capable.
This solution works for me. I expanded my question with a more in-depth example of what my data structure looks like.
the solution is helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.