Home  >  Article  >  Web Front-end  >  A brief discussion on the most nice way to pass parameters and obtain parameters in routing in angular4.0

A brief discussion on the most nice way to pass parameters and obtain parameters in routing in angular4.0

亚连
亚连Original
2018-05-31 10:01:091429browse

The editor below will share with you an article about the most nice way to pass parameters and obtain parameters in routing in angular4.0. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Researched the official website of ng4 and finally found the method I wanted. The result I want is to use '&' splicing parameters to send, which is the best for reading.

Otherwise, there are many ‘/’ splices, which can easily confuse parameters and component names.

Generally our page jump parameters are in this format:

http://angular.io/api?uid=1&username =moon

But in SPA single-page applications, the following results are mostly the result [Elementary videos are all perfunctory like this]

http://angular.io/api/1/moon

So how do you achieve the results I mentioned?

The main point begins.

Realize jumping from the product page to the product-detail page.

step1: Configure routing in app-routing.module.ts.

const routes: Routes = [
{
 path: 'product',
 component: ProductComponent,
 },
 {
 path: 'product-detail',
 component: ProductDetailComponent,
 }
];

#step2: Write a jump in product.ts and pass the parameters.

constructor(
 private router: Router, //这里需要注入Router模块
){}
jumpHandle(){
 //这是在html中绑定的click跳转事件
 this.router.navigate(['product-detail'], {
 queryParams: {
  productId: '1',
  title: 'moon'
 }
 });
}

#step3: Get the passed parameters productId and title## in product-detail.ts

#

constructor( 
 private activatedRoute: ActivatedRoute, //这里需要注入ActivatedRoute模块 
) { 
 activatedRoute.queryParams.subscribe(queryParams => { 
 let productId = queryParams.productId; 
 let title = queryParams.title; 
 }); 
}

ok, that’s the perfect solution.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the configuration method using sass based on css preloading in vue

Implemented in WeChat applet Sample code for finger zoom images

Paging based on vue.js

The above is the detailed content of A brief discussion on the most nice way to pass parameters and obtain parameters in routing in angular4.0. 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