Vue's method of intercepting strings

 

The string method in vue, I currently use the following two methods the most, because the string method of vue supports assertion operations. 1. The method of intercepting strings in vue is as follows: 2. The method of intercepting strings in vue also depends on the support provided by the vue library. 3. The method of intercepting strings in vue can be realized in vue-cli. For details, please refer to the following code: The above three methods of intercepting strings, we can choose to use according to our own needs. Different needs require different choices. The way. I will mainly introduce the third method here. In fact, all three methods can achieve string interception. 1. String interception in vue: We can use vue-cli to achieve it, or use vue-cli+ select to achieve it. In actual use, we need to pay attention to the following points: 1. The assertion operation in Vue is cumbersome and needs to rely on the support provided by the library. 2. In practical application, if you want to perform data manipulation or data verification, you need to choose a different way to achieve it. 3. If you have a familiar assertion class, the effect will be better in actual use. The above is my introduction to the method of intercepting strings in Vue. I hope to be helpful.

  • 1. Front-end data display

    There are many ways to display front-end data, and I will show them in two ways here. We can see that when we want to perform data operations, we format the data, and then filter or sort the data according to our needs. But in practice, we don't have to use formatted data. We can use some other ways to display. Here I take the npm package in Vue as an example to show the front-end data display. When we need to process an npm package, we can use the following code: In the above code, there are two functions: The first function receives the npm package parameters input by the user, and maps the contents of the npm package to the vue object , and then return the corresponding value; the second function is to operate on the content in the npm package, and return the corresponding value after the operation is realized. You can see that after the first function receives the npm package parameter input by the user, it will convert it into a numeric variable, then map this variable into the Vue object, and finally return the corresponding value. In the above code, there is an important part that needs to operate on the content in the npm package. But we don't need to use vue's built-in select method to achieve this. We can operate through the select method provided by Vue.

  • 2. Form data acquisition

    Now there are many application scenarios that need to obtain form data, such as obtaining the user's mobile phone number. If we directly use SQL to query, it will definitely take a lot of time and effort. At this time, we can directly use Vue. sql to get form data. In Vue, we only need to use sql statements to get form data. The following is a piece of code I wrote myself. Next, let's see how it works: Through the above code, we can see that there are three main steps when obtaining form data: 1. Use SQL statements to obtain data from the database. 2. Use sql statements to perform query operations. 3. Store the query result in a variable and print it out. Through the above three steps, we can save the obtained data in variables and print them out. This is how sql is used.

  • 3. User-defined

    User-defined refers to the custom operation of data through the Vue API. In functional programming, users can call other methods through function methods to achieve other functions. For example, in vue, we can get a certain user's data through select, then we can get a certain user's data through select+ webpack (), or we can directly define a function in functional programming to get a certain user data. For example: the following code is the user-defined function we defined: In the above code, we can use select to obtain the data of a certain user. In this way, we can achieve: In the above code, we only need to implement the first two methods, but if we want to implement the latter two methods, we need to use user-defined functions to obtain data.

  • 4. Data verification

    We all know that data verification can be achieved through signature. If we want to verify the data, we can actually use vue-cli to do it. The signature method we mentioned above can be used to verify strings. In Vue, there are two ways to verify strings: 1. Select in vue: This is the method we are most familiar with. It is mainly used for string verification. This method is the simplest and the most commonly used one. Ways of identifying. 2. vue-cli: In fact, this method is similar to the select method, and we need to implement it in vue-cli.

  • 5. Data comparison

    Here, let's take a look at the function of this function by comparing the data of one month. During this period, we have three variables, namely datetime, datetime and datetime.random, where the datetime variable is the stored string content, the datetime variable is the stored date string content, and the last one is the string we just intercepted up. By comparing this function, we can see that the data of each month is different. In this way, we can intuitively see the different data of each month. And this function has another advantage, that is, you can directly compare datetime and datetime. If you use this function during data validation, it will allow you to perform very fine-grained validation of the data.

  • 6. Data cleaning

    After we complete the data processing, we find that there are errors in the data, such as missing values, duplicate values, and failure to meet the conditions. At this time, data cleaning is required. If we just want to do simple data cleaning, we can do it through json. In json, what we need to deal with is: 1. First determine whether the missing value exists: if the missing value does not exist, return false. 3. Returns true if the duplicate value does not exist. The processing method of json is the same as that of java.lang.Memory, which is to judge whether it is empty. In practice, we can use post or requestMapping to process data.

  • 7. Exception handling

    Vue's exception handling is also very powerful. Of course, when using exception handling, you must pay attention to several aspects: 1. Do not add exceptions to functions, especially functions that encapsulate exceptions themselves. 2. If the return value of your function is null, then be sure to check whether the function is empty. 3. If you want to use the exception handling function, please add the corresponding type to the function parameter, or use the vue_exception package. 4. Do not use the function. throw () method to handle exceptions, this method will only throw an error.

The following are several commonly used codes for Vue to intercept strings:

1. Use the substr() method of JavaScript:
```javascript
// intercept the first 10 characters
{ { str.substr(0, 10) }}
```
2. Use the slice() method of JavaScript:
```javascript
// Intercept from the 5th character to the 10th character
{ { str.slice(4, 10) }} ``
`
3. Use Vue's filter:
```javascript
// Define the filter
Vue.filter(' truncate', function (value, length) { if (!value) return '' value = value.toString() if (value.length > length) { return value.substring(0, length) + '...' } else { return value } }) // use filter { { str | truncate(10) }} ```











4. Use JavaScript regular expressions:
```javascript
// intercept the first 10 characters
{ { str.replace(/^(.{10}).+/, '$1...') }} ``
`
The above are some commonly used codes for Vue to intercept strings, and you can choose the method that suits you according to your actual needs.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130923154