[問卦] Angular 18,分享當前網頁的網址連接?

作者: ruthertw (小葉老大)   2024-06-05 20:59:38
公司長官說他很熟javascript,
他跑去跟客戶承諾, 鬆一下:https://www.youtube.com/watch?v=4bIR6Xt768g
然後依照客戶要求,
其中一項,
是要開發分享當前網頁的網址,
也就是分享當前網頁網址連接到社群網站或APP的功能.
應該就是:https://reurl.cc/4r8Y7D
左手邊"分享本文"的功能.
鈣磷量!
公司裡面只有一位員工,
8年前寫過javascript,
公司長官要他用最新框架Angular 18,
跟其他同事一起寫完這項功能.
該同事問長官有什麼套件可以用?
長官說,
你自己查,
別問我啦!
我們有查到javascript套件,
可是沒人會用,
也猜不出 https://reurl.cc/4r8Y7D
左手邊"分享本文"的功能,
是用什麼套件寫的?
再拿套件去問長官,
阿長官你不是說你好熟javascript?
知不知道到底是哪個套件?
長官說,
他要下班去跟老闆用餐,
叫我們自己弄出來...
該位同事說他從Angular官方教學架好站,用ChatGPT,硬刻出以下內容:
[index.html]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A18</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script async defer crossorigin="anonymous"
src="https://connect.facebook.net/en_US/sdk.js"></script>
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<div ng-controller="MainController">
<button ng-click="shareOnFacebook()">Share on Facebook</button>
</div>
<script src="app.js"></script>
</body>
</html>
[app.js]
angular.module('myApp', [])
.controller('MainController', ['$scope', '$location', function($scope,
$location) {
$scope.shareOnFacebook = function() {
var currentUrl = $location.absUrl();
var facebookShareUrl =
'https://www.facebook.com/sharer/sharer.php?u=' +
encodeURIComponent(currentUrl);
window.open(facebookShareUrl, '_blank', 'width=600,height=400');
alert("yes!")
};
}]);
[app.component.ts]
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
export class AppComponent {
title = 'sshare!';
constructor() {
(window as any).fbAsyncInit = function() {
FB.init({
appId : '123456789',
cookie : true,
xfbml : true,
version : 'v11.0'
});
};
(function(d, s, id){
let js: HTMLScriptElement, fjs = d.getElementsByTagName(s)[0] as
HTMLScriptElement;
if (d.getElementById(id)) {return;}
js = d.createElement(s) as HTMLScriptElement; js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
if (fjs && fjs.parentNode) {
fjs.parentNode.insertBefore(js, fjs);
}
}(document, 'script', 'facebook-jssdk'));
}
shareOnFacebook() {
FB.ui({
method: 'share',
href: window.location.href,
}, function(response: any){});
}
}
[app.module.ts]
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ShareButtonsModule } from '@ngx-share/buttons';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ShareButtonsModule.withConfig({
debug: true
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
[app.component.html]
<style>
:host {

Links booklink

Contact Us: admin [ a t ] ucptt.com