Home  >  Article  >  Web Front-end  >  Let’s talk about the global method of uniapp navigation bar button

Let’s talk about the global method of uniapp navigation bar button

PHPz
PHPzOriginal
2023-04-23 09:10:281428browse

With the rapid development of mobile applications, more and more developers are beginning to use Uniapp based on Vue.js to develop cross-platform applications. The Uniapp navigation bar button is also a very commonly used component in applications. In applications, it is often necessary to add buttons to the right or left side of the navigation bar to implement functions such as jumps and operations. This article will introduce how to use global methods to control navigation bar buttons in Uniapp.

1. Define the global navigation bar method

In the Uniapp framework, the navigation bar button may be used on each page. In order to facilitate management, we can define it globally .

We create a new js file and name it common.js for managing global methods.

function setNavigationBarRightBtnText(text) {
  uni.setNavigationBarRightButton({
    text: text
  })
}

function setNavigationBarLeftBtnText(text) {
  uni.setNavigationBarLeftButton({
    text: text
  })
}

export default {
  setNavigationBarRightBtnText,
  setNavigationBarLeftBtnText
}

In the above code, we defined two global methods setNavigationBarRightBtnText and setNavigationBarLeftBtnText, which are used to modify the display copy of the buttons on the right and left sides of the navigation bar respectively. Here we use the uni.setNavigationBarRightButton and uni.setNavigationBarLeftButton methods to set it.

2. Call the global navigation bar method

In the page where the navigation bar button needs to be used, we can use the following code to call the above global method:

<script>
import common from '@/common.js';
export default {
  mounted() {
    common.setNavigationBarRightBtnText('按钮文案') // 修改右侧导航栏按钮的文案
    common.setNavigationBarLeftBtnText('按钮文案') // 修改左侧导航栏按钮的文案
  }
};
</script>

Import in the page common.js and call the setNavigationBarRightBtnText and setNavigationBarLeftBtnText methods to modify the display text of the navigation bar button. Here we call the method in mounted and execute it after the page rendering is completed.

3. Conclusion

In Uniapp development, the global method can easily and uniformly manage the navigation bar buttons. By defining global methods, we can avoid duplicating code and improve development efficiency. The above is a brief introduction to using global methods to control navigation bar buttons. Hope it helps everyone.

The above is the detailed content of Let’s talk about the global method of uniapp navigation bar button. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn