目標:建立一個導覽頁腳元件來顯示我的 X、LinkedIn 和 Github 頁面。
npm i -save-exact bootstrap-icons
我安裝了 bootstrap-icons
將 svg 圖示複製到頁尾元件。
ng g c components/navFooter --flat
打開 Components/nav-footer.component.ts 並新增版權年份。
export class NavFooterComponent {
Year = signal(new Date().getFullYear());
}
在範本中渲染 SVG 圖示、文字和 url
template: `
<footer class="p-2 h-full flex flex-col justify-center">
<div class="flex justify-between">
<div>
<p>Copyright @ {{ year() }} - All right reserved.</p>
</div>
<div class="flex flex-row basis-1/4">
<a href="https://github.com/railsstudent" target="_blank" aria-describedby="Github" class="grow shrink basis-1/3">
<span class="mr-2">Github</span>
<svg class="inline-block" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-github inline-block" viewBox="0 0 16 16">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27s1.36.09 2 .27c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0 0 16 8c0-4.42-3.58-8-8-8"/>
</svg>
</a>
<a href="https://x.com/connieleung404" target="_blank" aria-describedby="X" class="grow shrink basis-1/3">
<span class="mr-2">X</span>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-twitter-x inline-block" viewBox="0 0 16 16">
<path d="M12.6.75h2.454l-5.36 6.142L16 15.25h-4.937l-3.867-5.07-4.425 5.07H.316l5.733-6.57L0 .75h5.063l3.495 4.633L12.601.75Zm-.86 13.028h1.36L4.323 2.145H2.865z"/>
</svg>
</a>
<a href="https://www.linkedin.com/in/connieleung107/" target="_blank" aria-describedby="Linkedin" class="grow shrink basis-1/3">
<span class="mr-2">Linkedin</span>
<svg class="inline-block" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-linkedin inline-block" viewBox="0 0 16 16">
<path d="M0 1.146C0 .513.526 0 1.175 0h13.65C15.474 0 16 .513 16 1.146v13.708c0 .633-.526 1.146-1.175 1.146H1.175C.526 16 0 15.487 0 14.854zm4.943 12.248V6.169H2.542v7.225zm-1.2-8.212c.837 0 1.358-.554 1.358-1.248-.015-.709-.52-1.248-1.342-1.248S2.4 3.226 2.4 3.934c0 .694.521 1.248 1.327 1.248zm4.908 8.212V9.359c0-.216.016-.432.08-.586.173-.431.568-.878 1.232-.878.869 0 1.216.662 1.216 1.634v3.865h2.401V9.25c0-2.22-1.184-3.252-2.764-3.252-1.274 0-1.845.7-2.165 1.193v.025h-.016l.016-.025V6.169h-2.4c.03.678 0 7.225 0 7.225z"/>
</svg>
</a>
</div>
</div>
<div>
<p>Powered by <a href="https://analogjs.org/" target="_blank">AnalogJS</a></p>
</div>
</footer>
`
範本比較雜亂,隔天我會把svg元素重構成svg圖示元件。
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { NavBarComponent } from './components/nav-bar.component';
import { NavFooterComponent } from './components/nav-footer.component';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, NavBarComponent, NavFooterComponent],
template: `
<blog-nav-bar class="navbar" />
<div class="main">
<router-outlet></router-outlet>
</div>
<blog-nav-footer class="nav-footer" />
`,
})
export class AppComponent {}
將 NavFooterComponent
附加到導入 imports
並在範本中顯示新元件。
styles: `
:host {
display: grid;
grid-template-rows: 60px 1fr 250px;
grid-template-columns: 1fr;
grid-template-areas: "header"
"body"
"footer"
}
.navbar {
grid-area: header;
}
.main, router-outlet + * {
grid-area: body;
}
.nav-footer {
grid-area: footer;
}
`,
在瀏覽器上導航至 http://localhost:5173/,新的頁腳將會新增到頁面底部。
Github Repo: