Get the App
SLTechnology News&Howtos  ›  Development  › 

How does vue get the background json string?

Shulou Source: shulou.com Published: 2022-06-01 00:27:19 10月04日 Update

This article mainly explains "what is the way vue gets the background json string". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn "what is the way for vue to obtain background json strings".

Vue gets the background json string

1. Write a function to get json under the driver.js file under the api file

Export const tree = () = > request ({url: 'driver/tree', method:' post',})

two。 Introduce the tree function under the driver.js file into the index.vue file under the driver file under view file

Import Tree, {tree} from'@ / api/driver'

3. Get json in the initial process of page rendering under the lifecycle function created hook, get it through the arrow function, and pay attention to the json format. If a string is sent from the background, convert it to an object type through the function.

Created () {tree () .then ((str) = > {console.log (111111); / trigger the lifecycle function / / console.log (str); / / the backend gives a string and converts it to the object type let obj = JSON.parse (str); / / passes the json string to the data data, and then renders the this.data = obj on the page / / console.log (obj); console.log (obj [0] .id); / / View the id value under the first tier}

4. Finally, we should pay attention to formatting and assigning the initial value of json in the data data.

Data () {return {data: / / first give the data object empty [{}, {}, {}], / / default information that the tree control needs to configure, matching defaultProps: {children: 'childs', label:' name',}} with json

5. To view the objects at this layer in the console, you need to specify the level with parentheses in []

Console.log (obj [0] .id); / / View the id value at the first level, the conversion between Vue string and Json conversion string and array

1. Convert a string to an array

Str.split (','); / / A split string with a comma

two。 Convert an array to a string

Arr.join (','); / / concatenates array items into strings separated by commas to convert Json strings into json objects

1. Use eval

Result = eval ('('+ jsonstr +')'); / / jsonstr is the json string

two。 Use JSON.parse ()

Result = JSON.parse (jsonstr); / / jsonstr is a json string

The difference between eval and JSON.parse:

Eval is supported by javascript, and data can be converted without strict json format.

JSON.parse is a conversion method supported by browsers. You must have a standard json format to convert it.

Thank you for your reading, the above is the content of "what is the way vue gets the background json string?" after the study of this article, I believe you have a deeper understanding of what the way vue acquires background json string is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Character string background mode function file object array format hierarchy data learning content cycle periodic function life type comma page support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MariaDB macOS Shulou Tech Info