L
L
lolrofl012018-11-12 21:28:06
Vue.js
lolrofl01, 2018-11-12 21:28:06

Why is only number or json passed to vue component?

I'll start with the code, the blade templating engine:

<single-news
                    :news="{{ json_encode($news) }}"
                    :url="123"
            ></single-news>

The second url parameter passes only numbers and json, if you pass any string - it will be undefined, if the php function is any other than json_encode - the script will crash and display nothing at all.
Here is the component code:
export default {
        props: [
            'news',
            'url'
        ],
        mounted() {
            console.log('Check: ' + this.url);
        }
    }

Why is this happening? I need to pass a string to url

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-11-12
@lolrofl01

Why is this happening?

Because you are using dynamic data transfer. Accordingly, the passed string is either treated as the name of a property of the current component, or is incorrect garbage. Hence undefined (there is no property) and "the script will crash" (due to the garbage value of the parameter, the template could not be compiled).
Well, numbers and json, they are correct expressions: a number is a number, and json is treated as an object/array literal.
Pass a static value, or wrap the value in quotes.

P
Philip Gaponenko, 2018-11-13
@filgaponenko

:url=" 'stroka' "

spoiler
spangebob_raibow.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question