gpt4 book ai didi

javascript - 如何使用 typescript 和组件式语法将 google 身份验证添加到 vue.js

转载 作者:行者123 更新时间:2023-12-03 06:48:12 25 4
gpt4 key购买 nike

我正在尝试将 google 身份验证添加到我的 vue.js 前端。这是一个通过 CLI 创建的新项目,启用了 typescript 和组件样式语法(以及其他),并且还有一个相应的后端 Web 服务器。我一直在关注这个guide来自谷歌,但我对 vue 和 gapi 很陌生。我不知道如何加载<script src="https://apis.google.com/js/platform.js" async defer></script>进入我的应用程序或加载后如何使用它。我找到了像 this 这样的例子在 jsfiddle 上以及堆栈溢出和其他论坛上的一些其他示例,但似乎没有一个使用 typescript 和组件样式语法或不完整。

App.vue

<template>
<div id="app">
<div id="nav">
<router-link to="/">Login</router-link>
</div>
<router-view />
</div>
</template>

<style lang="scss">
</style>

主要.ts

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

Vue.config.productionTip = false;

new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");

Login.vue( View )

<template>
<div>
<Login />
</div>
</template>

<script>
// @ is an alias to /src
import Login from "@/components/Login.vue";

export default {
name: "login",
components: {
Login
}
};
</script>

Login.vue(组件)

<template>
<div>
<button>Sign in with Google</button>
</div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";

@Component
export default class Login extends Vue {}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>

最佳答案

那么,您需要在 public 文件夹的 index.html 中添加 google 登录脚本。

Make sure you add it in the head section and without async and defer mode.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="https://apis.google.com/js/api:client.js"></script>
</head>
<body>
<noscript>
<strong
>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work
properly without JavaScript enabled. Please enable it to
continue.</strong
>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

现在在你的 login.vue 文件中

<template>
<div>
<button class="g-signin-button">Sign in with Google</button>
</div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";
@Component
export default class Login extends Vue {
clientId = "AIzaSyBRxykObiOjM7VsY_lyGcRU27q8aFeAagk";
mounted() {
if (!window.gapi) {
throw new Error(
'"https://apis.google.com/js/api:client.js" needs to be included as a <script>.'
);
}

if (!this.clientId) {
throw new Error("Client Id must be specified.");
}

window.gapi.load("auth2", () => {
const auth2 = window.gapi.auth2.init({ client_id: this.clientId });
auth2.attachClickHandler(
this.$refs.signinBtn,
{},
googleUser => {
this.$emit("success", googleUser);
},
error => {
this.$emit("error", error);
this.$emit("failure", error); // an alias
}
);
});
}
methods() {
function err(msg: string) {
typeof console !== "undefined" &&
console.error(`[g-signin-button] ${msg}`);
}
}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.g-signin-button {
box-sizing: border-box;
position: relative;
/* width: 13em; - apply for fixed size */
margin: 0.2em;
padding: 0 15px 0 46px;
border: none;
text-align: left;
line-height: 34px;
white-space: nowrap;
border-radius: 0.2em;
font-size: 16px;
color: #fff;
background: #dd4b39;
}
.g-signin-button:before {
content: "";
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
width: 34px;
height: 100%;

border-right: #bb3f30 1px solid;
background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/14082/icon_google.png")
6px 6px no-repeat;
}

.g-signin-button:hover,
.g-signin-button:focus {
cursor: pointer;
background: #e74b37;
}
</style>

由于 gapi 安装在窗口级别并且支持 typescript,因此需要为类型安装外部 npm 包

npm i -D @types/gapi @types/gapi.auth2

并将其添加到 tsconfig.json 中的 types 键中

"types": ["webpack-env", "gapi", "gapi.auth2"],

希望这对您有所帮助!

关于javascript - 如何使用 typescript 和组件式语法将 google 身份验证添加到 vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60367946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com