Home  >  Article  >  Web Front-end  >  How to jump to a specified location in uniapp

How to jump to a specified location in uniapp

PHPz
PHPzOriginal
2023-04-24 14:48:202090browse

UniApp is a cross-platform development framework that can be used to quickly build highly reusable mobile applications. In UniApp, we can jump and locate between different pages through some simple methods. In this article, I will introduce to you how UniApp jumps to a specified location.

In UniApp, you can use the $router.push and $router.replace methods provided by vue-router to implement the jump. Both methods can take two options: params (parameters) and query (query). Among them, params is the dynamic path parameter defined in the routing configuration, and query is the parameter passed in the routing jump.

For example, we have this piece of code in the routing:

{
  path: '/demo/:id',
  name: 'Demo',
  component: () => import('@/pages/demo.vue')
}

In this routing, we define a dynamic path parameter named id. When jumping to the route in the page, we can pass parameters through the $router.push method. As shown below:

this.$router.push({
  name: 'Demo',
  params: {
    id: '123'
  }
})

In this way, the page jump is realized, and the parameter id is passed to the Demo component.

If we need to jump to a different location on the current page, we can use anchor points to achieve this. An anchor is an identifier for an HTML element that can be used to locate and jump to that element. For example, we can add an element with the id anchor to the page and give it a unique identifier.

<div id="anchor"></div>

Then, when jumping, we can add the fragment identifier of #anchor to the jump URL to jump to the element.

this.$router.push('/demo/123#anchor')

In this way, you can jump to the element position with the id anchor in the Demo component.

In addition to using anchor points, UniApp also provides some other methods to achieve page positioning. For example, we can achieve page positioning by getting the offsetTop of the element in the component's created life cycle.

created() {
  this.$nextTick(() => {
    let target = document.getElementById('anchor')
    let scrollTop = target.offsetTop
    document.documentElement.scrollTop = scrollTop
    document.body.scrollTop = scrollTop
  })
}

This will automatically jump to the specified location when the page is loaded.

In short, in UniApp, jumping to a specified location can be achieved using anchor points, routing parameters and other methods. Developers can choose the most suitable method to achieve page positioning based on the actual situation.

The above is the detailed content of How to jump to a specified location in uniapp. 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