HTML源代码
<div ng-app="">
<div ng-controller="test">
<div ng-address-bar browser="html5"></div>
<br><br>
$location.url() = {{$location.url()}}<br>
$location.search() = {{$location.search('keyword')}}<br>
$location.hash() = {{$location.hash()}}<br>
keyword valus is={{loc}} and ={{loc1}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
AngularJS源代码
<script>
function test($scope, $location) {
$scope.$location = $location;
$scope.ur = $scope.$location.url('www.html.com/x.html?keyword=test#/x/u');
$scope.loc1 = $scope.$location.search().keyword ;
if($location.url().indexOf('keyword') > -1){
$scope.loc= $location.url().split('=')[1];
$scope.loc = $scope.loc.split("#")[0]
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这里变量loc
和loc1
返回测试作为上述URL的结果.这是正确的方法吗?
我有如下路由器链接:
<button class="take-a-tour-btn" [routerLink]="['/dashboard', {'showTour':'show'}]">
Run Code Online (Sandbox Code Playgroud)
我想传递参数showTour
.但是,当我这样做时,参数是可见的url
,我想隐藏它.我已经经历了这么多的引用(关于可选参数的说法),但在我的案例中没有成功.我该怎么解决这个问题?
我不能app.use
在Express 4.16.2上使用路由参数.
我有这样的声明:
app.use('/course-sessions/:courseSessionId/chapter-sessions', chapterSessionsRouter)
Run Code Online (Sandbox Code Playgroud)
并且,在章节会话路由器:
// Index.
router.get('/', ...resource.indexMethods)
Run Code Online (Sandbox Code Playgroud)
当我得到时'/course-sessions/0/chapter-sessions/'
,我有404.
然后我尝试这样的声明:
app.get('/course-sessions/:courseSessionId/chapter-sessions', ...chapterSessionResource.indexMethods)
Run Code Online (Sandbox Code Playgroud)
现在我得到了我想要的结果.因此路由参数不起作用app.use
.
在我研究的过程中,我得到了这个GitHub问题以及这个拉动请求来解决这个问题.但似乎这个问题仍然存在.或者我做错了什么?
我有 NestJS 应用程序,我必须从 URL 获取查询。问题是我的creationDate是一个对象,我无法通过@Query将其作为嵌套对象获取。
以下是路线示例:
xxx/yyy/orders?key1=value&key2=value&key3=value&createdAt[lte]=2021-07-01&createdAt[gte]=2011-03-30&orderBy=desc&limit=500
Run Code Online (Sandbox Code Playgroud)
我正在尝试将createdAt[lte]和createdAt[gte]作为嵌套对象
export interface QueryI {
key1: string;
key2: string;
key3: string;
key4: string;
createdAt: {
lte: string,
gte: string,
}
orderBy: 'desc' | 'asc';
limit: number;
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
@Get('route')
getAll(
@Query() query: QueryI): Promise<void> {
return this.myService.findAll(query);
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下结果
{
key1: 'value',
key2: 'value',
key3: 'value',
key4: 'value',
'createdAt[lte]': 'some date',
'createdAt[gte]': 'some date',
orderBy: 'desc',
limit: '500'
}
Run Code Online (Sandbox Code Playgroud)
我尝试过 JSON.stringify 并在 SOW 上咨询了类似的问题,但没有运气。
谢谢
我无法将路由参数检索为空。我正在使用 angular7。请找到下面的代码
HeaderComponent ts 文件
import {Router, ActivatedRoute} from '@angular/router';
constructor(private httpService: HttpClient, private router: Router, private activatedRoute: ActivatedRoute) {
this.getNewsSelectedFromRouteParam();
}
getNewsSelectedFromRouteParam() {
alert();
let id = this.activatedRoute.snapshot.paramMap.get('typeId');
alert(id);
}
getNewsByCountry(newsTypeSelected: any) {
this.router.navigate(['/news',newsTypeSelected]);
this.getNewsSelectedFromRouteParam();
}
Run Code Online (Sandbox Code Playgroud)
标题html
<div class=" dropdown-toggle" data-toggle="dropdown">
XXX
</div>
<div class="dropdown-menu">
<div *ngFor="let cr of country" class="dropdown-item"
(click)="getNewsByCountry(cr.value)" >{{cr.name}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
应用路由ts文件
const routes: Routes = [
{ path: 'news/:typeId', component: XxxComponent },
{ path: '**', component: XxxComponent }
];
Run Code Online (Sandbox Code Playgroud) <a href="#"
[routerLink]="[isTrue?'/location-1':'/location-2', '']"
[queryParams]="{id:loc.id,l:'loc'}">
Run Code Online (Sandbox Code Playgroud)
isTrue
这里当值为true
/location-1/?id=1&l=loc时
但是当值为 时isTrue
,false
我需要
/location-2/?id=1&l=loc2&idL=2
那么如何有条件地修改参数呢?
我们在代码中使用了路由参数。但是在 jasmine karma 中执行单元测试时,我们遇到了以下错误。
TypeError: Cannot read property 'params' of undefined
TypeError: Cannot read property 'params' of undefined
at Object.<anonymous> (http://localhost:9876/base/src/test.ts:38586:20)
at ZoneDelegate.713.ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts:1599:26)
at ProxyZoneSpec.onInvoke (http://localhost:9876/base/src/test.ts:82238:39)
at ZoneDelegate.713.ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts:1598:32)
at Zone.713.Zone.run (http://localhost:9876/base/src/polyfills.ts:1359:43)
at Object.<anonymous> (http://localhost:9876/base/src/test.ts:81952:34)
at attemptSync (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1950:24)
at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1938:9)
at http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1962:16
at http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1905:9
at http://localhost:9876/base/src/test.ts:14790:17
at ZoneDelegate.713.ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts:1599:26)
at AsyncTestZoneSpec.onInvoke (http://localhost:9876/base/src/test.ts:81547:39)
at ProxyZoneSpec.onInvoke (http://localhost:9876/base/src/test.ts:82235:39)
at ZoneDelegate.713.ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts:1598:32)
Run Code Online (Sandbox Code Playgroud)
请在下面找到我们的 ts 文件:
import { Component, OnInit, AfterViewInit, EventEmitter, Output } from '@angular/core';
export class MyComponent implements OnInit, AfterViewInit { …
Run Code Online (Sandbox Code Playgroud) unit-testing karma-jasmine karma-coverage routeparams angular
我想在我的 Vue.JS 应用程序中使用vue-router
这样的查询参数:
http://localhost:8080/#/home/name=Alice&surname=Smith
Run Code Online (Sandbox Code Playgroud)
我已将此路线添加到我的routes
中router.ts
:
routes: [{
path: '/home/name=:name&surname=:surname',
name: 'home',
component: Home
}]
Run Code Online (Sandbox Code Playgroud)
我还想做以下两件事:
1)我希望能够有参数name
,并surname
以任意顺序。例如,这两个 URL 应该导致相同的视图:
http://localhost:8080/#/home/name=Alice&surname=Smith
http://localhost:8080/#/home/surname=Smith&name=Alice
Run Code Online (Sandbox Code Playgroud)
2)我希望它们也是可选的。
有没有办法做这些事情?谢谢!
我设置了这条路线:
@Get('/name/like')
findByLikeName(@Query() query: { supplierID: string; name: string }) {
return this.supplierProductService.findByLikeName(query);
}
Run Code Online (Sandbox Code Playgroud)
它利用底层服务中的查询参数:
async findByLikeName({
supplierID,
name,
}: {
supplierID: string;
name: string;
}): Promise<SupplierProduct[]> {
return await this.supplierProductRepository.findAll({
where: {
name: {
[Op.like]: `%${name}%`,
},
supplierID: supplierID,
},
});
}
Run Code Online (Sandbox Code Playgroud)
但是,假设我想将 sellerID 移动到 /:supplierID 路由参数中,同时在查询对象中维护名称(以及潜在的其他查询参数),我将如何实现这一点?
如何在angular2.my路由配置的路由中定义可选参数,如下所示:
<a [routerLink]="['../abc',{xyz: blabla}]">
and
<a [routerLink]="['../abc']">
{ path: '/abc/:xyz', component: abc, name: 'abc' }, // Here i want xyz as optional perameter
Run Code Online (Sandbox Code Playgroud)
所以问题是每当我使用第一个方法与参数blabla
它工作正常,因为在路由时我已定义参数xyz
但在第二种方法的情况下我不想发送参数所以URL变为
HTTP://本地主机:8080 /#/ sideNav/ABC /
这是第一次罚款,但刷新页面后没有任何显示整个窗口变得空白,没有内容.那么有什么方法可以在angular2中路由时提供可选参数.
i have also tried without defining parameters like this
{ path: '/abc', component: abc, name: 'abc' }
Run Code Online (Sandbox Code Playgroud)
但是如果1
它转换1
成xyz的值,这将抛出错误true
我是expressjs的新手,对expressjs中的路由和这些概念了解不多.在阅读它时,我看到人们可以设置一条路线
route1 = app.get("/:param",callback)
Run Code Online (Sandbox Code Playgroud)
其中param将成为路由参数变量,所有这样的获取请求如:"/ foo"或"/ bar"将对应于该路由.
我的问题是:我现在可以有一条路线吗?
route2 = app.get("/param", callback)
Run Code Online (Sandbox Code Playgroud)
要么
app.get("/anyOtherRoute",callback)
Run Code Online (Sandbox Code Playgroud)
如果是这样,我怎么知道请求是针对route1而不针对route2(反之亦然)?
我有这个路由设置:
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController@index');
Route::get('/products', 'ProductsController@index');
Route::get('/orders', 'OrdersController@index');
});
Run Code Online (Sandbox Code Playgroud)
因此,如果我使用“操作”助手生成 url,则不必明确提供 storeId。
{{ action('DashboardController@index') }}
Run Code Online (Sandbox Code Playgroud)
如果提供,我希望从请求 URL 自动设置 storeId。
也许是这样的。
Route::prefix('admin/{storeId}')->group(function ($storeId) {
Route::get('/', 'DashboardController@index');
Route::get('/products', 'ProductsController@index');
Route::get('/orders', 'OrdersController@index');
})->defaults('storeId', $request->storeId);
Run Code Online (Sandbox Code Playgroud) routeparams ×12
angular ×5
express ×2
javascript ×2
nestjs ×2
routing ×2
angular7 ×1
angularjs ×1
laravel ×1
laravel-5 ×1
node.js ×1
parameters ×1
query-string ×1
router ×1
typescript ×1
unit-testing ×1
url-routing ×1
vue-router ×1
vue.js ×1