Route away from existing content
I created a new Angular 7 app using the Angular CLI. I left the configuration/setup the default and started adding my code on-top of it. The AppComponent
makes a service call to fetch some blog posts and pass them to a child component that renders them.
app.component.html
<div class="container">
<header class="header-site">
<p class="site-title">Sully<p>
<p class="site-tagline">Code-monkey</p>
<p class="site-description">A sotware development blog with a touch of tech and a dash of life.</p>
</header>
<div *ngFor='let post of posts'>
<app-blog-post [post]="post"></app-blog-post>
</div>
</div>
<router-outlet></router-outlet>
app.component.ts
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
posts: Post;
constructor(private blogService: BlogService) {}
async ngOnInit() {
await this.loadPosts();
}
async loadPosts() {
this.posts = await this.blogService.getPosts();
}
}
This loads the first 10 posts and renders them with my app-blog-post
child component.
blog-post-component.html
<article>
<header>
<h1 class="post-title"><a [routerLink]="['/blog', post.path]">{{post.title}}</a></h1>
<h4 class="post-details">{{post.details}}</h4>
</header>
<div>
<markdown ngPreserveWhitespaces [data]="post.content">
</markdown>
</div>
</article>
blog-post-component.ts
@Component({
selector: 'app-blog-post',
templateUrl: './blog-post.component.html',
styleUrls: ['./blog-post.component.css']
})
export class BlogPostComponent implements OnInit {
@Input() post: Post;
}
What happens is that it renders the first 10 blog posts as expected. Now, in the app-routing.module.ts
class, I've added /blog/:id
as a route, which my child component routes to when you click the post title.
const routes: Routes = [
{
path: 'blog/:id',
component: BlogPostComponent
}
];
I don't understand the routing mechanics here. When I navigate to the post, via the router, nothing happens. I see the URL path change in the browsers URL bar, but the content doesn't change.
What I'm trying to do is replace the 10 posts in the current view with the single post that's already been fetched as the only post on the page, when I hit that specific route. I've read through the docs but can't tell how to replace the content already in the view, with a subset of that content using the component already created. I'm not sure if I have to move the rendering of 10 posts off to a unique route, and just keep the router-outlet
as the only element in the app.component.html
, and for the '/'
route, route to the component containing the top 10 posts. I'm worried that makes sharing the post data I've already fetched, between sibling components, more difficult as the parent now has to push/pull between the two children. Is that a backwards way of handling it?
Further still, each blog post has a unique route. I'm porting my blog from an existing hosted service where the path to a post is /blog/year/month/day/title. Can my Routes object be as simple as /blog/:year/:month/:day/:title
? Since I can't get the routing working, I'm unable to test the routing itself and see if that's doable. I want to keep the same routing so existing bookmarks and search engine result links aren't broken.
javascript angular routing
add a comment |
I created a new Angular 7 app using the Angular CLI. I left the configuration/setup the default and started adding my code on-top of it. The AppComponent
makes a service call to fetch some blog posts and pass them to a child component that renders them.
app.component.html
<div class="container">
<header class="header-site">
<p class="site-title">Sully<p>
<p class="site-tagline">Code-monkey</p>
<p class="site-description">A sotware development blog with a touch of tech and a dash of life.</p>
</header>
<div *ngFor='let post of posts'>
<app-blog-post [post]="post"></app-blog-post>
</div>
</div>
<router-outlet></router-outlet>
app.component.ts
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
posts: Post;
constructor(private blogService: BlogService) {}
async ngOnInit() {
await this.loadPosts();
}
async loadPosts() {
this.posts = await this.blogService.getPosts();
}
}
This loads the first 10 posts and renders them with my app-blog-post
child component.
blog-post-component.html
<article>
<header>
<h1 class="post-title"><a [routerLink]="['/blog', post.path]">{{post.title}}</a></h1>
<h4 class="post-details">{{post.details}}</h4>
</header>
<div>
<markdown ngPreserveWhitespaces [data]="post.content">
</markdown>
</div>
</article>
blog-post-component.ts
@Component({
selector: 'app-blog-post',
templateUrl: './blog-post.component.html',
styleUrls: ['./blog-post.component.css']
})
export class BlogPostComponent implements OnInit {
@Input() post: Post;
}
What happens is that it renders the first 10 blog posts as expected. Now, in the app-routing.module.ts
class, I've added /blog/:id
as a route, which my child component routes to when you click the post title.
const routes: Routes = [
{
path: 'blog/:id',
component: BlogPostComponent
}
];
I don't understand the routing mechanics here. When I navigate to the post, via the router, nothing happens. I see the URL path change in the browsers URL bar, but the content doesn't change.
What I'm trying to do is replace the 10 posts in the current view with the single post that's already been fetched as the only post on the page, when I hit that specific route. I've read through the docs but can't tell how to replace the content already in the view, with a subset of that content using the component already created. I'm not sure if I have to move the rendering of 10 posts off to a unique route, and just keep the router-outlet
as the only element in the app.component.html
, and for the '/'
route, route to the component containing the top 10 posts. I'm worried that makes sharing the post data I've already fetched, between sibling components, more difficult as the parent now has to push/pull between the two children. Is that a backwards way of handling it?
Further still, each blog post has a unique route. I'm porting my blog from an existing hosted service where the path to a post is /blog/year/month/day/title. Can my Routes object be as simple as /blog/:year/:month/:day/:title
? Since I can't get the routing working, I'm unable to test the routing itself and see if that's doable. I want to keep the same routing so existing bookmarks and search engine result links aren't broken.
javascript angular routing
add a comment |
I created a new Angular 7 app using the Angular CLI. I left the configuration/setup the default and started adding my code on-top of it. The AppComponent
makes a service call to fetch some blog posts and pass them to a child component that renders them.
app.component.html
<div class="container">
<header class="header-site">
<p class="site-title">Sully<p>
<p class="site-tagline">Code-monkey</p>
<p class="site-description">A sotware development blog with a touch of tech and a dash of life.</p>
</header>
<div *ngFor='let post of posts'>
<app-blog-post [post]="post"></app-blog-post>
</div>
</div>
<router-outlet></router-outlet>
app.component.ts
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
posts: Post;
constructor(private blogService: BlogService) {}
async ngOnInit() {
await this.loadPosts();
}
async loadPosts() {
this.posts = await this.blogService.getPosts();
}
}
This loads the first 10 posts and renders them with my app-blog-post
child component.
blog-post-component.html
<article>
<header>
<h1 class="post-title"><a [routerLink]="['/blog', post.path]">{{post.title}}</a></h1>
<h4 class="post-details">{{post.details}}</h4>
</header>
<div>
<markdown ngPreserveWhitespaces [data]="post.content">
</markdown>
</div>
</article>
blog-post-component.ts
@Component({
selector: 'app-blog-post',
templateUrl: './blog-post.component.html',
styleUrls: ['./blog-post.component.css']
})
export class BlogPostComponent implements OnInit {
@Input() post: Post;
}
What happens is that it renders the first 10 blog posts as expected. Now, in the app-routing.module.ts
class, I've added /blog/:id
as a route, which my child component routes to when you click the post title.
const routes: Routes = [
{
path: 'blog/:id',
component: BlogPostComponent
}
];
I don't understand the routing mechanics here. When I navigate to the post, via the router, nothing happens. I see the URL path change in the browsers URL bar, but the content doesn't change.
What I'm trying to do is replace the 10 posts in the current view with the single post that's already been fetched as the only post on the page, when I hit that specific route. I've read through the docs but can't tell how to replace the content already in the view, with a subset of that content using the component already created. I'm not sure if I have to move the rendering of 10 posts off to a unique route, and just keep the router-outlet
as the only element in the app.component.html
, and for the '/'
route, route to the component containing the top 10 posts. I'm worried that makes sharing the post data I've already fetched, between sibling components, more difficult as the parent now has to push/pull between the two children. Is that a backwards way of handling it?
Further still, each blog post has a unique route. I'm porting my blog from an existing hosted service where the path to a post is /blog/year/month/day/title. Can my Routes object be as simple as /blog/:year/:month/:day/:title
? Since I can't get the routing working, I'm unable to test the routing itself and see if that's doable. I want to keep the same routing so existing bookmarks and search engine result links aren't broken.
javascript angular routing
I created a new Angular 7 app using the Angular CLI. I left the configuration/setup the default and started adding my code on-top of it. The AppComponent
makes a service call to fetch some blog posts and pass them to a child component that renders them.
app.component.html
<div class="container">
<header class="header-site">
<p class="site-title">Sully<p>
<p class="site-tagline">Code-monkey</p>
<p class="site-description">A sotware development blog with a touch of tech and a dash of life.</p>
</header>
<div *ngFor='let post of posts'>
<app-blog-post [post]="post"></app-blog-post>
</div>
</div>
<router-outlet></router-outlet>
app.component.ts
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
posts: Post;
constructor(private blogService: BlogService) {}
async ngOnInit() {
await this.loadPosts();
}
async loadPosts() {
this.posts = await this.blogService.getPosts();
}
}
This loads the first 10 posts and renders them with my app-blog-post
child component.
blog-post-component.html
<article>
<header>
<h1 class="post-title"><a [routerLink]="['/blog', post.path]">{{post.title}}</a></h1>
<h4 class="post-details">{{post.details}}</h4>
</header>
<div>
<markdown ngPreserveWhitespaces [data]="post.content">
</markdown>
</div>
</article>
blog-post-component.ts
@Component({
selector: 'app-blog-post',
templateUrl: './blog-post.component.html',
styleUrls: ['./blog-post.component.css']
})
export class BlogPostComponent implements OnInit {
@Input() post: Post;
}
What happens is that it renders the first 10 blog posts as expected. Now, in the app-routing.module.ts
class, I've added /blog/:id
as a route, which my child component routes to when you click the post title.
const routes: Routes = [
{
path: 'blog/:id',
component: BlogPostComponent
}
];
I don't understand the routing mechanics here. When I navigate to the post, via the router, nothing happens. I see the URL path change in the browsers URL bar, but the content doesn't change.
What I'm trying to do is replace the 10 posts in the current view with the single post that's already been fetched as the only post on the page, when I hit that specific route. I've read through the docs but can't tell how to replace the content already in the view, with a subset of that content using the component already created. I'm not sure if I have to move the rendering of 10 posts off to a unique route, and just keep the router-outlet
as the only element in the app.component.html
, and for the '/'
route, route to the component containing the top 10 posts. I'm worried that makes sharing the post data I've already fetched, between sibling components, more difficult as the parent now has to push/pull between the two children. Is that a backwards way of handling it?
Further still, each blog post has a unique route. I'm porting my blog from an existing hosted service where the path to a post is /blog/year/month/day/title. Can my Routes object be as simple as /blog/:year/:month/:day/:title
? Since I can't get the routing working, I'm unable to test the routing itself and see if that's doable. I want to keep the same routing so existing bookmarks and search engine result links aren't broken.
javascript angular routing
javascript angular routing
edited Nov 27 '18 at 2:10
Johnathon Sullinger
asked Nov 27 '18 at 1:57
Johnathon SullingerJohnathon Sullinger
4,09532265
4,09532265
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When you utilise the routing mechanism, the specified component gets rendered in the appropriate router-outlet
. I suspect that what you see when you navigate to a route, is the selected post rendered at the very bottom of the page.
As you allude to, if you would like to render the list of default blog posts, you will need to create a separate route/component for it, letting the router-outlet
render it for you.
The way the routing works, is by going through each entry in your routes list and checking the current path for a match. This means the order is important. If you want to track both /blog/:year/:month/:day/:title
and blog/:id
, you will want to order them most specific first. And if you want an empty URL to point to a landing page, as you have above you could put something like { path: '', pathMatch: 'full', [component/redirectTo] }
at the bottom of your route list.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491682%2froute-away-from-existing-content%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you utilise the routing mechanism, the specified component gets rendered in the appropriate router-outlet
. I suspect that what you see when you navigate to a route, is the selected post rendered at the very bottom of the page.
As you allude to, if you would like to render the list of default blog posts, you will need to create a separate route/component for it, letting the router-outlet
render it for you.
The way the routing works, is by going through each entry in your routes list and checking the current path for a match. This means the order is important. If you want to track both /blog/:year/:month/:day/:title
and blog/:id
, you will want to order them most specific first. And if you want an empty URL to point to a landing page, as you have above you could put something like { path: '', pathMatch: 'full', [component/redirectTo] }
at the bottom of your route list.
add a comment |
When you utilise the routing mechanism, the specified component gets rendered in the appropriate router-outlet
. I suspect that what you see when you navigate to a route, is the selected post rendered at the very bottom of the page.
As you allude to, if you would like to render the list of default blog posts, you will need to create a separate route/component for it, letting the router-outlet
render it for you.
The way the routing works, is by going through each entry in your routes list and checking the current path for a match. This means the order is important. If you want to track both /blog/:year/:month/:day/:title
and blog/:id
, you will want to order them most specific first. And if you want an empty URL to point to a landing page, as you have above you could put something like { path: '', pathMatch: 'full', [component/redirectTo] }
at the bottom of your route list.
add a comment |
When you utilise the routing mechanism, the specified component gets rendered in the appropriate router-outlet
. I suspect that what you see when you navigate to a route, is the selected post rendered at the very bottom of the page.
As you allude to, if you would like to render the list of default blog posts, you will need to create a separate route/component for it, letting the router-outlet
render it for you.
The way the routing works, is by going through each entry in your routes list and checking the current path for a match. This means the order is important. If you want to track both /blog/:year/:month/:day/:title
and blog/:id
, you will want to order them most specific first. And if you want an empty URL to point to a landing page, as you have above you could put something like { path: '', pathMatch: 'full', [component/redirectTo] }
at the bottom of your route list.
When you utilise the routing mechanism, the specified component gets rendered in the appropriate router-outlet
. I suspect that what you see when you navigate to a route, is the selected post rendered at the very bottom of the page.
As you allude to, if you would like to render the list of default blog posts, you will need to create a separate route/component for it, letting the router-outlet
render it for you.
The way the routing works, is by going through each entry in your routes list and checking the current path for a match. This means the order is important. If you want to track both /blog/:year/:month/:day/:title
and blog/:id
, you will want to order them most specific first. And if you want an empty URL to point to a landing page, as you have above you could put something like { path: '', pathMatch: 'full', [component/redirectTo] }
at the bottom of your route list.
answered Nov 27 '18 at 2:24
AvaAva
1479
1479
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491682%2froute-away-from-existing-content%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown