账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    angular13路由怎么使用url传参
    55
    0
    需求点击按钮后, 将文本框的值传递到output页面这是我原先的代码export class DemandComponent implements OnInit { constructor() { } ngOnInit(): void { } // ... data!:any;}<h3>页面传值</h3><input type="text" placeholder="请输入内容" value="" [(ngModel)]="data"><a routerLink="/demo/output" routerLinkActive="router-link-active" [queryParams]="{id:data}">传递输出页面</a>output.component.html 如何用插值来获取查询字符串?<p>output works!</p>在网上查过, 很多都是写成这样子import { Router } from '@angular/router';.constructor(private router: Router) {}.this.router.navigate(['/newscontent'],{ queryParams:{ name:'laney', id:id }, skipLocationChange: true //可以不写,默认为false,设为true时路由跳转浏览器中的url会保持不变,传入的参数依然有效});不是很理解, 为什么要在构造方法里定义ActivatedRoute? 然后在外面获取不应该是全局的吗?构造方法里定义的模板怎么用{{...}}获取?我用的是最新版的ts, 不知道是不是版本的问题
    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 孤星 普通会员 1楼

      在Angular13中,可以使用params属性来获取URL中的参数。这个属性返回一个对象,其中包含所有URL参数的键值对。

      以下是一个示例:

      ```typescript import { Component, OnInit } from '@angular/core';

      @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { constructor() { }

      ngOnInit() { // 获取URL中的参数 const params = this.$http.get('http://example.com/api/data?param1=value1&param2=value2').then(response => response.json()); console.log(params); } } ```

      在这个示例中,我们在app.component.html中使用了ngOnInit钩子来获取URL参数。然后,我们使用$http.get来发送GET请求到http://example.com/api/data,并将URL参数作为参数传递。最后,我们在console.log中打印出所有的参数。

      注意,$http是Angular13中的一个依赖注入容器,用于发送HTTP请求。在上面的示例中,我们使用了this.$http.get来发送GET请求,这意味着我们需要在其他组件中注入$http实例。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部