修改路径
This commit is contained in:
18
src/app/view/category/category-routing.module.ts
Normal file
18
src/app/view/category/category-routing.module.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {NgModule} from '@angular/core';
|
||||
import {RouterModule, Routes} from '@angular/router';
|
||||
import {CategoryComponent} from './category.component';
|
||||
|
||||
const routes: Routes = [
|
||||
{path: ':category', component: CategoryComponent},
|
||||
{path: '**', component: CategoryComponent}
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
RouterModule.forChild(routes)
|
||||
],
|
||||
exports: [RouterModule]
|
||||
})
|
||||
|
||||
export class CategoryRoutingModule {
|
||||
}
|
||||
28
src/app/view/category/category.component.html
Normal file
28
src/app/view/category/category.component.html
Normal file
@@ -0,0 +1,28 @@
|
||||
<div id="main">
|
||||
|
||||
|
||||
<div id="category">
|
||||
<ul *ngIf="categoryList.length">
|
||||
<c-tag-tag *ngFor="let category of categoryList"
|
||||
[tag]="{name:category.name,size:category.articles.length}"
|
||||
(tagClick)="changeCategory(category)">
|
||||
|
||||
</c-tag-tag>
|
||||
</ul>
|
||||
<h2 *ngIf="!categoryList.length">暂时没有分类</h2>
|
||||
</div>
|
||||
|
||||
<span id="tip">当前分类为 :
|
||||
<span style="font-weight: bolder;">{{name}}</span>
|
||||
</span>
|
||||
|
||||
<ul id="detail" *ngIf="articleList&&articleList.list.length">
|
||||
<c-article-detail-card *ngFor="let article of articleList.list"
|
||||
[data]="article" [showMediaArea]="false" [showTagArea]="false">
|
||||
</c-article-detail-card>
|
||||
</ul>
|
||||
|
||||
<div *ngIf="!articleList||!articleList.list.length">
|
||||
<h2 style="text-align: center">该分类暂无文章</h2>
|
||||
</div>
|
||||
</div>
|
||||
29
src/app/view/category/category.component.less
Normal file
29
src/app/view/category/category.component.less
Normal file
@@ -0,0 +1,29 @@
|
||||
#main {
|
||||
width: 75%;
|
||||
margin: 30px auto;
|
||||
border-radius: 10px;
|
||||
padding: 10px;
|
||||
background: #ffffff;
|
||||
|
||||
#tip {
|
||||
margin-left: 40px;
|
||||
font-size: large;
|
||||
}
|
||||
|
||||
#category {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#detail {
|
||||
margin-top: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (max-width: 940px) {
|
||||
#main {
|
||||
width: 100%;
|
||||
}
|
||||
#detail{
|
||||
margin: 10px 40px 10px 0;
|
||||
}
|
||||
}
|
||||
25
src/app/view/category/category.component.spec.ts
Normal file
25
src/app/view/category/category.component.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { CategoryComponent } from './category.component';
|
||||
|
||||
describe('CategoryComponent', () => {
|
||||
let component: CategoryComponent;
|
||||
let fixture: ComponentFixture<CategoryComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ CategoryComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(CategoryComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
71
src/app/view/category/category.component.ts
Normal file
71
src/app/view/category/category.component.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {ApiService} from '../../api/api.service';
|
||||
import {Tag} from '../../class/Tag';
|
||||
import {NzMessageService} from 'ng-zorro-antd';
|
||||
import {PageList} from '../../class/HttpReqAndResp';
|
||||
import {Article} from '../../class/Article';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {Location} from '@angular/common';
|
||||
import {Title} from '@angular/platform-browser';
|
||||
|
||||
@Component({
|
||||
selector: 'view-category',
|
||||
templateUrl: './category.component.html',
|
||||
styleUrls: ['./category.component.less']
|
||||
})
|
||||
export class CategoryComponent implements OnInit {
|
||||
|
||||
constructor(private apiService: ApiService,
|
||||
private nzMessageService: NzMessageService,
|
||||
private activatedRoute: ActivatedRoute,
|
||||
private location: Location,
|
||||
private title: Title) {
|
||||
}
|
||||
|
||||
categoryList: Tag[] = [];
|
||||
private category: Tag;
|
||||
articleList: PageList<Article>;
|
||||
|
||||
name: string;
|
||||
|
||||
ngOnInit() {
|
||||
this.name = this.activatedRoute.snapshot.paramMap.get('category');
|
||||
this.getCategories(this.name == null);
|
||||
if (this.name != null) {
|
||||
this.getArticles(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
getCategories(needGetArticle: boolean) {
|
||||
this.apiService.categories().subscribe(data => {
|
||||
this.categoryList = data.result;
|
||||
this.category = data.result[0];
|
||||
if (needGetArticle) {
|
||||
this.getArticles(this.category.name);
|
||||
this.name = this.category.name;
|
||||
this.title.setTitle('小海博客 | 分类 | ' + this.name);
|
||||
}
|
||||
}, error => {
|
||||
this.nzMessageService.error('出现了错误,原因:' + error.msg);
|
||||
});
|
||||
}
|
||||
|
||||
getArticles(categoryName: string) {
|
||||
this.apiService.articlesByCategory(categoryName).subscribe(data => {
|
||||
this.articleList = data.result;
|
||||
}, error => {
|
||||
this.nzMessageService.error('出现了错误,原因:' + error.msg);
|
||||
});
|
||||
}
|
||||
|
||||
changeCategory(category: Tag) {
|
||||
if (this.name === category.name) {
|
||||
return;
|
||||
}
|
||||
this.category = category;
|
||||
this.name = category.name;
|
||||
this.location.replaceState('categories/' + this.name);
|
||||
this.getArticles(this.name);
|
||||
this.title.setTitle('小海博客 | 分类 | ' + this.name);
|
||||
}
|
||||
}
|
||||
20
src/app/view/category/category.module.ts
Normal file
20
src/app/view/category/category.module.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import {NgModule} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {CategoryComponent} from './category.component';
|
||||
import {CategoryRoutingModule} from './category-routing.module';
|
||||
import {NzIconModule, NzToolTipModule} from 'ng-zorro-antd';
|
||||
import {IndexModule} from '../index/index.module';
|
||||
|
||||
|
||||
@NgModule({
|
||||
declarations: [CategoryComponent],
|
||||
imports: [
|
||||
CommonModule,
|
||||
CategoryRoutingModule,
|
||||
NzToolTipModule,
|
||||
NzIconModule,
|
||||
IndexModule
|
||||
]
|
||||
})
|
||||
export class CategoryModule {
|
||||
}
|
||||
Reference in New Issue
Block a user