


With the popularity of mobile applications, more and more developers are turning their attention to uniapp, a cross-platform development tool. It is based on the Vue.js framework, which can achieve the effect of writing once and running on multiple platforms. However, when using uniapp to develop mobile applications, you will also encounter some problems. Today, we will discuss the problem that the uniapp filtering attribute does not take effect on iOS.
- Problem phenomenon
In uniapp, we can use filters to format our data. For example, using {{time | dateFormat}} in the template can convert time into the date format we want. The code is as follows:
<template> <view>{{ date | dateFormat }}</view> </template> <script> export default { data() { return { date: '2022-01-01 12:00:00' } }, filters: { dateFormat(val) { return val.split(' ')[0] } } } </script>
When running on the Android side, "2022-01-01" can be output normally. But on the ios side, the original "2022-01-01 12:00:00" will be output, and the filter does not seem to take effect.
- Analysis of reasons
After investigation and analysis, we found that this problem is caused by the JS engine on the ios side improperly handling regular expressions in ES6 syntax. Specifically, Said that the problem lies in the way the uniapp filtering attribute is implemented.
In Vue.js, filters are implemented through Vue.prototype.$filter. In uniapp, this global variable is defined in uni-app/dist/vue.runtime.esm.js. In Vue.js, there is another way to implement filters, which is to use the Vue.component() method to pass an object that contains a filters property. This method is also applicable in uniapp, but the display effect on the iOS side is different from the above method and can be output normally.
- Solution
Now that we find that the problem lies in the implementation of the uniapp filtering attribute, the solution is ready to come out. For this problem, we have the following two solutions:
(1) Use the Vue.component() method to define the filter
<script> export default { data() { return { date: '2022-01-01 12:00:00' } }, created() { Vue.component('dateFormat', { filters: { dateFormat(val) { return val.split(' ')[0] } } }) } } </script>
In the above code, we use Vue.component () method to define filters. After testing, this method can output normally on the ios side.
(2) Use regular expressions to solve the problem
In uniapp development, we can also use regular expressions to solve the problem that the filter does not take effect on the ios side. We can use the replace() method in the filter to replace non-numeric characters with null characters and then output a string in the specified format. The code is as follows:
<script> export default { data() { return { date: '2022-01-01 12:00:00' } }, filters: { dateFormat(val) { return val.replace(/[^\d]/g, '').substring(0, 8) } } } </script>
In this code, we use the regular expression /1/g to match non-numeric characters and replace() method to replace it with the null character. Finally, use the substring() method to get the string output in the specified format. Similarly, after testing, this method can also output normally on the ios side.
In short, in uniapp development, there is no need to panic when encountering such problems. You only need to deeply analyze the nature of the problem and adopt correct and effective solutions, and the problem will be solved.
- \d ↩
The above is the detailed content of What should I do if the uniapp filtering attribute does not take effect on iOS?. For more information, please follow other related articles on the PHP Chinese website!

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
